Jonas Stawski

Everything .NET and More

Restart your Elevated Trust, Out-Of-Browser (OOB) Silverlight App

With the release of Silverlight 4 RC you can now close your app programmatically, but unfortunately, you can’t restart it. Why would you want to restart it? Because when the app finds a new update, the update won’t take place until the next restart of the application. In some cases that is ok, but with all the new features of Silverlight 4 for OOB and the support for touch, more and more people are utilizing Silverlight for Kiosk applications, where the application shouldn’t rely on a user to restart it.

Fortunately when you are running on Elevated Trust and OOB you can use the AutomationFactory, which “provides access to the Automation servers.” In Windows that means you get access to COM+, therefore allowing you to do anything COM+ can do. As of this writing, this feature is not available for Mac.

So to restart our app we can use the AutomationFactory to execute a WScript.Shell command by calling the SLLauncher. Unfortunately for us, the SLLauncher requires some arguments that are unknown at runtime and you can see those arguments if you right click on your desktop shortcut – Properties. The big number is a random number that Silverlight generates when the app is first installed and there is no API to retrieve it, so before we can call the SLLauncher command we need to retrieve it by using… yep, you guessed it, the AutomationFactory. Bellow is the code:

image

   1: private string GetSLLauncherCommand()
   2: {
   3:     string desktopPath;
   4:     string SLLauncherCommand = "";
   5:     using (dynamic wShell = AutomationFactory.CreateObject("WScript.Shell"))
   6:     {
   7:         desktopPath = wShell.SpecialFolders("Desktop");
   8:     }
   9:     using (dynamic shell = AutomationFactory.CreateObject("Shell.Application"))
  10:     {
  11:         dynamic folder = shell.NameSpace(desktopPath);
  12:         dynamic items = folder.Items();
  13:         foreach (dynamic item in items)
  14:         {
  15:             if (item.IsLink)
  16:             {
  17:                 string fileName = item.Name.ToLower();
  18:                 if (fileName.Contains("appname"))
  19:                 {
  20:                     dynamic link = item.GetLink();
  21:                     SLLauncherCommand = "\"" + link.Path + "\" " + link.Arguments;
  22:                 }
  23:             }
  24:         }
  25:     }
  26:  
  27:     return SLLauncherCommand;
  28: }

WARNING: note that this code takes into account that the desktop shortcut of the application will always be there. If you can’t count on that then you should check the start menu. Don’t forget to add more robust checking.

So we first use WScript.Shell to get the user’s desktop path by calling the SpcialFolders method. once we have that, we use the Shell.Application objects to get a hold of the desktop shortcut by iterating over all icons and selecting the apps shortcut (.lnk). Once we find the file we use the GetLinkmethod to be able to retrieve the path and the arguments of the shortcut. Since this operation is very expensive, I recommend saving the command in IsolatedStorage or in memory. The next step is the actual restart and we do so with the following code:

   1: public static void StartAgain(string SLLauncherCommand)
   2: {
   3:     if (!String.IsNullOrEmpty(SLLauncherCommand))
   4:     {
   5:         try
   6:         {
   7:             using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
   8:             {
   9:                 shell.Run(SLLauncherCommand);
  10:             }
  11:  
  12:         }
  13:         catch { } //eat the exception
  14:     }
  15: }

Don’t forget to close your app after calling the StartAgain() method by calling

   1: Application.Current.MainWindow.Close();

Happy Programming!

Comments (6) -

Hey Now Jonas,

Nice Post, some good info on restarting OOB Sliverlight apps.

Thx 4 the info,
Catto

Reply

Jonas, first of all thanks for the solution.

Almost a half of the year passed since this post was published. Did you find a way to restart Silverlight app programmaticly on Mac? ..remove dependency on parsing it's shortcut?

Reply

@Koistya, I haven't found a better solution. Sorry.

Reply

Thanks !!! this is really useful

Reply

Nice One!!

Reply

Hi Jonas,
Did you find a way to restart Silverlight Out of Browser with Silverlight 5?
Thanks for the solution.

Reply

Add comment

biuquote
Loading