Page 1 of 1

Silverlight 4 - Using basic Shell commands

#1 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Posted 14 August 2010 - 06:32 AM

I've created this tutorial on using some basic shell commands with Silverlight 4 COM Interop services. These are just some things I have come across and solved myself, which may be useful to others!

Here we are going to launch a program and pass through a username/password from the OOB app.

Create a Silverlight Application project in Silverlight4. Uncheck the option to host as a web app.

First - some things you'll need to do before a successful build:
Right click project, add reference. Choose System Core under .NET and say ok.

Posted Image

Right click project, add reference. Browse to 'Microsoft SDKs\Silverlight\v4.0\Libraries\Client\Microsoft.CSharp.dll', and say ok.

Posted Image

Right click project, go to Properties. Check 'Enable Out-of-Browser settings'. Click the button Out-Of-Browser settings.

Posted Image

Scroll down and check 'show install menu' and 'require elevated trust'. Save all.

Posted Image

Here is a simple xaml page with a button that says "Open Program".

Posted Image

Mainpage.xaml
<UserControl x:Class="MyClientTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel>
            <Button x:Name="OpenProgram" Content="Open Program" Click="OpenProgram_Click"  />
        </StackPanel>
    </Grid>
</UserControl>





Mainpage.xaml.cs

Here I am going to open a sample test textfile with notepad.

Copy the code below into your MainPage.cs file. Take note to keep your own Namespace name. You may need to import System.Runtime.InteropServices.Automation

Posted Image



using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.InteropServices.Automation;

namespace MyClientTest
{
    /*Note: ComAutomationFactory has been renamed to 
    AutomationFactory in Silverlight4
*/
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnInstall_Click(object sender, RoutedEventArgs e)
        {
            if (Application.Current.InstallState == InstallState.NotInstalled)

                Application.Current.Install();
        }

        private void OpenProgram_Click(object sender, RoutedEventArgs e)
        {     
             using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
            {
                 shell.Run(@"C:\Windows\notepad.exe C:\Users\yourusername\Desktop\test.txt");
            }
        }

    }
}



Build and run the code. It should open like this

Posted Image

Click the button, and notepad opens with your chosen txt file loaded

Posted Image

Some things to note:
Here I used a single pair of double quotes when opening notepad.exe, but in my workplace I have encountered that
when opening a program file within shell.Run() from C:\Program Files\ you may need to surround it with three double quotes. ("'s).

Example:
using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
            {
                shell.Run(@"""C:\Program Files\Program\programfile.exe """);
            }



And when passing parameters through along with the program file executable, it can be achieved in the following way. In this example, I am passing through a username and a password.

using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
            {
                shell.Run(@"""C:\Program Files\Program\programfiles.exe """ + "-username user" + "-password password");
            }


This post has been edited by irishgirl: 15 August 2010 - 06:19 AM


Is This A Good Question/Topic? 2
  • +

Replies To: Silverlight 4 - Using basic Shell commands

#2 Guest_Kunal Chowdhury*


Reputation:

Posted 14 August 2010 - 09:30 PM

Nice Tutorial. Keep it up. Also read more about Silverlight COM Interoperability here:
http://www.kunal-cho...nteroperability
Was This Post Helpful? 2

#3 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Posted 15 August 2010 - 04:10 AM

Thank you :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1