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.

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

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

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

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

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

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

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

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





MultiQuote



|