Wednesday, June 23, 2010

How to: Adding Uninstall start menu item to your .NET deployment project

Its pretty simple. Follow the steps to achieve this.

  • Add the following code snippet  to your projects main method before InitializeComponent();
    string[] arguments = Environment.GetCommandLineArgs();
    foreach (string argument in arguments)
    {
       string[] parameters = argument.Split('=');
       if (parameters[0].ToLower() == "/u")
       {
         string productCode = parameters[1];
         string path = Environment.GetFolderPath(Environment.SpecialFolder.System);
         Process proc = new Process();
         proc.StartInfo.FileName = string.Concat(path, "\\msiexec.exe");
         proc.StartInfo.Arguments = string.Concat(" /i ", productCode);
         proc.Start();
       }
    }



    • Now go to your deployment project and go to the file system editor and right click on File System on Target Machine and Add Special Folder with name User's Programs Menu.


    • Add an additional shortcut to your primary output project and name it Uninstall.


    • Right Click  Shortcut to Set the Arguments property to /u=[ProductCode].


    • Now Build your Deployment setup project. Your are ready to go.


    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. If you want the product to remove only, replace the "/i " with "/x ".

    No comments:

    Post a Comment