Create an Uninstall Shortcut for Windows Installer in C#

When utilizing the windows installer project type bundled with Visual Studio I was very surprised to find there was no simple functionality to add an uninstall shortcut.

Instead, there are a few workarounds, some better than others but overall going to have to get your hands dirty to make this one work.

Option 1 – Batch File

Create an uninstall batch file and add a shortcut which points to the batch.

Only downside of this solution, which is actually the simplest, is an ugly little bugger we all know as the DOS window pops up when you execute the batch.

(courtesy of tech.chitgoks.com)

@echo off
msiexec /x {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

Option 2 – VBS File

No code example for this option since I didn’t even consider trying this one personally, but this would be to create the equivalent of the code above using WshShell in vbscript (which is actually a similar end result as the code in option 3).

The .vbs file would execute cleanly without a DOS prompt, but my experience with .vbs files has found that they are not AntiVirus friendly and don’t always port OS’es cleanly either. Certain API calls slightly change over time and can break syntax, and you never know if John Doe’s newest antivirus update will flag your .vbs file and prevent it from executing.

Option 3 – Roll up those sleeves

Create a separate app which calls msiexec either by instantiating a hidden cmd.exe process window, or calling it directly also using process object. Then add this separate app to your deployment project solution and create a shortcut in the installer which points to the primary output of your separate app project.

Using this method is the most work, but the best end result for the consumer IMO (isn’t that usually how it works? :P). If you wanted, you could even create your own uninstall process and run msiexec in “unattended” a.k.a “silent” mode and uninstall your app quietly in the background.

(courtesy of endofstream.com)

using System.Diagnostics; //namespace containing process RD

public partial class App : Application
{
  void Application_Startup(object sender,StartupEventArgs e)
  {
    for(int i = 0;i != e.Args.Length;++i)
    {
      if(e.Args[i].Split('=')[0].ToLower() == "/u")
      {
        string guid = e.Args[i].Split('=')[1];
        string path = Environment.GetFolderPath(Environment.SpecialFolder.System);
        ProcessStartInfo uninstallProcess = new ProcessStartInfo(path+"\\msiexec.exe","/x "+guid);
        Process.Start(uninstallProcess);
        System.Windows.Application.Current.Shutdown();
      }
    }
  }
}

Keep in mind in all of these scenarios, the application guid is necessary to pass as a parameter to msiexec. To pass this variable you will need to specify the productcode as a parameter in custom action data.

Such as:

CustomActionData => /productcode = [ProductCode]

I believe [ProductCode] may also be case sensitive, as I had originally specified “PRODUCTCODE” in one of my installers but this did not seem to pass through..

However…

If you’re still reading by this far (and yes I tried to blow up however to draw your eyes), keep in mind your uninstall executable or batch is calling msiexec, not the other way around, so “ProductCode” is not yet part of the mix.

I’ve yet to find the cleanest solution to this final issue, but overall my thoughts would be to pass product code through to a different separate application added to your deployment project solution as part of the install process, and have this application either store the product code in a generic location such as:

Environment.SpecialFolders.ApplicationData

…or, still using your “install helper” application, store it somewhere in the application directory in program files created by the installer. Essentially the same thing as application data, just leaves less chance of any traces of your program being left behind on uninstall.

Enjoy. 😉

References
http://tech.chitgoks.com/2009/02/06/visual-studio-create-an-uninstaller-shortcut-in-your-installer-wizard/
http://endofstream.com/creating-uninstaller-in-a-visual-studio-project/
Windows Installer (Relevant links for Custom Actions), http://msdn.microsoft.com/en-us/library/aa368066(v=vs.85).aspx, http://msdn.microsoft.com/en-us/library/2w2fhwzz%28v=vs.71%29.aspx, http://msdn.microsoft.com/en-us/library/2w2fhwzz%28v=vs.80%29.aspx, http://msdn.microsoft.com/en-us/library/aa368066%28v=VS.85%29.aspx, http://msdn.microsoft.com/en-us/library/aa367457%28VS.85%29.aspx
MSIexec command line options, http://msdn.microsoft.com/en-us/library/aa372024%28v=vs.85%29.aspx
Flexera Software, http://community.flexerasoftware.com/archive/index.php?t-128559.html

Advertisements

About Ronnie Diaz

Ronnie Diaz is an enterprise software engineer responsible for front-end and back-end development for companies in many industries. Heavily involved in cloud development, online retail, e-commerce and electronic ordering, fulfillment and customer relational systems.

Posted on March 31, 2011, in Programming & Development and tagged , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: