Creating uninstaller in a Visual Studio project
When you are creating a Setup Project you would almost always want to include a method for uninstalling the application. On this topic the Setup Project Wizard in Visual Studio is not entirely as helpful as it could have been, therefor I have a few solutions – that might be of help to you.
The way of the bat!
Many places on various blogs it is suggested to use a batch(or .bat) file. Although this method works just fine it does have one big minus, the command prompt is displayed and stands in the background like a big ugly black blob
. However if you would like to have a try at this solution, the code for the .bat file is shown below:
@echo off
msiexec /x {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
The more elegant way!
Another option is to make your application read command line arguments, the code below is from a WPF project but it should be fairly easy to twist to your needs:
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();
}
}
}
}
What happens in the code above is that the application searches through the supplied arguments and looks for the “/u” argument, when found a new process is spawned using the msiexec.exe file.
To add the uninstall link in the deployment project you just need to follow the steps below:
- Select your deployment project and go to the file system editor.
- Using the file system editor you navigate to the desired location of the install file.
- From the decired location you create a new shortcut to your primary output and name it Uninstall – or whatever else you would like.
- In the properties of the new shortcut you set the arguments property to /u=[ProductCode]
That is it you now have a functional uninstaller – without the black blob that is the command prompt.
|
|








September 20th, 2010 at 19:29
Thank you for this tutorial. It is unclear to me though how you create the unistall shortcut. The setup program program created by the publishing wizard does not create, upon being run, a folder for the application in C:\Program Files. Where exactly should the uninstall shortcut be created and what folder/file should it point to? Thanks.
September 20th, 2010 at 20:13
Hello tr00don
I am not certain that I understand your question correctly, but as I understand you – the problem is in creating the shortcut.
The location of the shortcut itself is irrelevant, and for you to decide (in my projects I usually place them in the “User’s Program Menu/[ProductName]” folder). But the linking of the file is the issue, to create the shortcut you navigate to whichever folder you have placed your “Primary output from [ProductName]” and right click this Output object and select “Create new shortcut to primary output…” this shortcut is the one mentioned in step 3 in the guide.
Did that answer the question?
Best regards
/Peter
March 31st, 2011 at 18:07
[...] http://endofstream.com/creating-uninstaller-in-a-visual-studio-project/ Windows Installer (Custom Actions), http://msdn.microsoft.com/en-us/library/aa368066(v=vs.85).aspx [...]
July 18th, 2011 at 01:07
This is a great post! Thank you.
November 29th, 2011 at 12:44
I am using Windows desktop project (Not WPF project).
Please tell me how we can have similar application_startup code in Windows desktop deployment project.
I am using .Net 4.0 ( but not a WPF project).
Whether it is possible?
December 5th, 2011 at 11:23
Hi Pack
Yes it is possible to do in Forms projects as well, just change the Main function to include arguments like this:
static void Main(string[] args)
{
foreach (string arg in args)
{
MessageBox.Show(“arg: “+arg);
}
}
Then you call the program with command line parameters and they will be included as you can see
Best Regards
/Peter
December 20th, 2011 at 16:33
HI Peter,
Thanks a ton for the reply.
By Main function you mean the main entry point of the apllication, right? But i am noyt sure whether this function will be executed on installing this application.
My case is like :- On installing the application every time i want to force uninstalling the previous version and install it freshly (even when the version number is not changed).
I tried the code you just suggested , and on debugging on that i did not found the argument ,
/U =[ProductCode] which we are expecting .
Here with this code we have ‘string[] args ‘ , but in the WPF code it is ‘ StartupEventArgs e’.
Whether these StartupEventArgs and simply ‘string[] args’ will match? I am not getting the ‘/U’ in the args list.
I have created the uninstall link as you told in the 4 step process.
Please suggest a way.
Thanks in advance.
December 21st, 2011 at 10:48
Hi Pack
If what you want is to uninstall the application – and then install it again. This method will not work, this is just for uninstalling.
I am not wuite sure that I understand what you are truing to do, and installing the program is usually done with the installer – and not from the application itself (if that can even be done).
As for the setup project – it can remove the existing application (you can have a look at “RemoveExistingProducts Action”: http://msdn.microsoft.com/en-us/library/windows/desktop/aa371197(v=vs.85).aspx)
I have tested the arguments in a forms project that takes string[] args, and it worked here as described – Is it a forms application you are trying to install, or something else?
I would ask that we continue the dialog over mail as you suggest, so as not to spam too many comments here
Best Regards
/Peter