Click here to Skip to main content
15,910,877 members
Articles / Operating Systems / Windows
Article

Add an uninstall start menu item to your .NET deployment project

Rate me:
Please Sign up or sign in to vote.
4.66/5 (26 votes)
21 Aug 2005CPOL 265.6K   77   54
The simple way to add an uninstall menu item to your .NET deployment project

Introduction

It's super easy to add this to your deployment project.

  1. Select your deployment project and go to the file system editor, user programs menu.
  2. Add an additional shortcut to your primary output project and name it Uninstall Application.
  3. Set the Arguments property to /u=[ProductCode].
  4. Add the following code to your project's Main() sub or startup form's New() sub just before the call to InitializeComponent().
    VB
    Dim arguments As String() = Environment.GetCommandLineArgs()
    Dim argument As String
    For Each argument In arguments
        If argument.Split("=")(0).ToLower = "/u" Then
            Dim guid As String = argument.Split("=")(1)
            Dim path As String = _
               Environment.GetFolderPath(Environment.SpecialFolder.System)
            Dim si As New ProcessStartInfo(path & _
                      "\msiexec.exe", "/i " & guid)
            Process.Start(si)
            Close()
            Application.Exit()
            End
        End If
    Next

That's is! The Deployment project will replace [ProductCode] in the Arguments property with the actual installer project's ProductCode GUID value. Your program will see the /u={Actual ProductCode} argument and pass it to msiexec.exe before exiting.

The installer is run in repair/remove mode for your application. The user is allowed to select repair or remove, and continue. If you want the product to remove only, replace the "/i " with "/x ".

License

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


Written By
Systems Engineer Mark J Means Consulting
United States United States
I have been a software consultant since 1985 working on everything from the Commodore VIC-20 & RadioShack CoCo games to 8051 Embedded USB Microcontrollers to Windows Vista database applications. I have written over a half million lines of code since 2004. Please see my DataConnectionDialog control at http://mjmeans.com/dcd.aspx.

Comments and Discussions

 
GeneralRe: works but... Pin
mjmeans22-Aug-05 13:22
mjmeans22-Aug-05 13:22 
GeneralRe: works but... Pin
JabraJabra22-Aug-05 22:35
JabraJabra22-Aug-05 22:35 
GeneralRe: works but... Pin
John Kang23-Aug-05 4:41
John Kang23-Aug-05 4:41 
GeneralRe: works but... Pin
JabraJabra23-Aug-05 21:43
JabraJabra23-Aug-05 21:43 
GeneralRe: works but... Pin
John Kang24-Aug-05 0:09
John Kang24-Aug-05 0:09 
GeneralRe: works but... Pin
JabraJabra24-Aug-05 0:35
JabraJabra24-Aug-05 0:35 
GeneralRe: works but... Pin
mjmeans24-Aug-05 21:03
mjmeans24-Aug-05 21:03 
GeneralRe: works but... Pin
rrue11-Sep-08 8:27
rrue11-Sep-08 8:27 
I'm having similar trouble in a c# app.

Tried embedding the uninstall code in the main form and depending on where I put it I either get:

* A working uninstall that still launches the app.
* An uninstall that crumps halfway through. Shortcuts disappear from the desktop, start menu, and quick launch, but the program files folder remains and if I go to add/remove programs the app is still there (and can be removed successfully there).
* An uninstall that immediately crumps on launch. Kicks up that helpful "wouldn't you like to send this information to Microsoft but damned little information for you is actually in it" message.

Finally separated the uninstall code into a separate console app and added that exe to the deployment package. Using the same argument structure for convenience. Works.

Note that this one uninstalls without prompts but does show a progress bar. Look up the command line arguments for msiexec.exe if you want this otherwise.

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace uninstall
{
    class uninstall
    {
        static void Main(string[] args)
        {
            string[] arguments = Environment.GetCommandLineArgs();
            foreach (string arg in arguments)
            {
                if (arg.Split('=')[0].ToLower() == "/u")
                {
                    string guid = arg.Split('=')[1];
                    string path = Environment.GetFolderPath(System.Environment.SpecialFolder.System);
                    string str = path + "\\msiexec.exe";
                    ProcessStartInfo pi = new ProcessStartInfo(str);
                    pi.Arguments = "/passive /uninstall " + guid;
                    pi.UseShellExecute = false;
                    Process.Start(pi);
                }
            }
        }
    }
}

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.