Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / file

How to launch external application or file from Silverlight 4

3.00/5 (2 votes)
7 Sep 2010CPOL 26.4K  
Launching external application or file from Silverlight 4 (out-of-browser) using Automation Factory

Problem



I have been recently trying to automate the instalation of COM server for my Silverlight app and I found it pretty hard to find any solution. The thing is I wanted to run the downloaded setup file from Silverlight code, which turned out to be not so easy in Silverlight application.


Solution


Reading KunalChowdhury's article (File Explorer using Silverlight 4 COM Interoperability), I have noticed he pointed out it is possible using COM.
After quite annnoying search I have finally found SpoonStomper's post on Silverlight forum.
Original solution was for JavaScript using ActiveX but, as I have checked immediately, it works also with AutomationFactory in Silverlight 4.


C#
private void Run(string path)
{
    try
    {
        dynamic shell= AutomationFactory.CreateObject("WScript.shell");
        shell.run(path);
     }
     catch (System.Exception)
     {
         MessageBox.Show("failure");
     }
}

The Run method creates shell object and runs application or file with path given in param. It allows to launch any external application or open any file with program related to this file.

WScript reference can be found here.

This solution will work only in out-of-browser mode with elevated permisions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)