Wednesday, July 10, 2013

Create a uninstall link for a .Net application in the Start program folder in Windows platform

Normally when an application is installed in the windows platform, in its Start program folder, user always expects a uninstall link that user can click to uninstall the application. By default, though, the .Net Setup project in VS2010 does not create such a link automatically for the user when it packages the .Net application. However, this can be done easily. 

Step 1: Create and configure Setup project for the .Net Application
Create a setup project for the .Net application. This is straightforward.

After the Setup project is created and properly configured for the .Net application, right-click the setup project in the VS2010 IDE, and selects View->File System from the popup context menu. When the file system of the setup project is opened, double click "Application Folder" on the right panel. This will show the list of files to be in the program folder when the application is installed on the user's computer. 

Now look for a file named "Primary output from XXX (Active)" (where XXX refers to the name of the .Net application project), right-click and select "Create Shortcut to Primary output from XXX (Active)", this will creates a shortcut to the .net application software. renamed this shortcut to "Uninstall". 

Now right-click the "Uninstall" shortcut and selects "Properties Window", in the "Properties Window", enter "/u=[ProductCode]" for the "Arguments" property of the "Uninstall" shortcut. 

Next in the "User's Programs Menu", create a folder with the name of the .Net application project, let's say "XXX". Now cut and paste the "Uninstall" shortcut from the "Application Folder" to "User's Programs Menu/XXX" folder. With this step the configuration for the setup project is completed. But we need to also tells the .Net application that if it receives the "/u" arguments then it should perform uninstallation. This is done by adding codes to the .Net application project's Program.cs file.

Step 2: Modify Program.cs in .Net Application for unstallation
Open the Program.cs in the .Net application project, change the codes in the Main() to the following

        static void Main()
            bool uninstalling = false;
            //uninstaller part of the code
            string[] arguments= Environment.GetCommandLineArgs();
            foreach(string argument in arguments)
                 if(argument.Split('=')[0].ToLower() == "/u")
                     uninstalling = true;
                    string guid = argument.Split('=')[1];
                    string path = Environment.GetFolderPath(Environment.SpecialFolder.System);
                    System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo(path + "\\msiexec.exe", "/i " + guid);

            if (uninstalling == false)
                //if it is not unstalling, then run the normal code for starting the .Net application, for example, the following
                Application.Run(new MainForm());

This completes the necessary details for creating the unistall link

No comments:

Post a Comment