Every once in a while there comes a need to provide a mechanism to uninstall your application programmatically.   For Silverlight Out-Of-Browser applications , there seemed to be no way to accomplish it.  Fortunately, there’s a work-around that trusted OOB applications running in Windows can use.

Silverlight OOB applications are launched via the “sllauncher.exe” command.  And if you open the properties on an OOB shortcut, you will see a target textbox with something like:

"C:Program Files (x86)Microsoft Silverlightsllauncher.exe" 2205248641.localhost

Of course, “sllauncher.exe” has some command-line parameters and settings we can use to install and uninstall Silverlight OOB applications.  Since trusted Silverlight OOB applications can access COM Automation, we can run the “sllauncher.exe” application from within Silverlight.  The catch is that we’re not sure exactly where the program is located.

There may be exceptions, but as a general rule “sllauncher.exe” is located in 1 of 2 places:

on 64-bit machines : C:Program Files (x86)Microsoft Silverlightsllauncher.exe

on 32-bit machines : C:Program FilesMicrosoft Silverlightsllauncher.exe

However, there’s no way to know which OS type you’re running on.  So we need to check for the existence of these folders.  Since they’re out of the sandbox, we have to resort to COM Automation to determine if they exist.

   1: string launcherPath = string.Empty;
   2: using (dynamic shell = AutomationFactory.CreateObject("Shell.Application"))
   3: {
   4:     string launcher64 = @"C:Program Files (x86)Microsoft Silverlight";
   5:     string launcher32 = @"C:Program FilesMicrosoft Silverlight";
   6:  
   7:     dynamic folder64 = shell.NameSpace(launcher64);
   8:     if (folder64 != null) launcherPath = launcher64;
   9:  
  10:     dynamic folder32 = shell.NameSpace(launcher32);
  11:     if (folder32 != null) launcherPath = launcher32;
  12: }

To silently uninstall a Silverlight OOB application using “sllauncher.exe” you’d normally type something like:

sllauncher.exe /uninstall /origin:[origin location]

Once again, using COM Automation, we can have our application run the command.

   1: using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
   2: {
   3:     string origin = Application.Current.Host.Source.OriginalString;
   4:     shell.Run(string.Format(@"{0}/sllauncher.exe /uninstall /origin:""{1}""", launcherPath, origin));
   5: }

That’s really all there is to it (other than adding in some error checking, of course).