Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / operating-systems / Windows

An Uninstaller To Uninstall Any .NET Application

3.05/5 (7 votes)
2 Aug 2007CPOL3 min read 1   1.5K  
Create an Uninstaller to Uninstall Any Installed .NET Application

Introduction

I have named this utility as uninstaller because this utility can be used to uninstall any .NET application which is deployed using .NET deployment wizard [Windows Installer].

Background

I was fedup by uninstalling .NET application from AddRemove programs. I wanted something like a standard uninstaller so you just need to double click and it will uninstall your application.

You can create your uninstaller using Orca, a utility to update your MSI, but that is not a simple task. It is very complicated and whenever you build your setup you have to do the same thing again and again.

So I am here to help out people who don't know how to create standard uninstaller for their .NET application.

Using the Code

To use this utility, first you have to create a setup project in .NET. After adding Primary output of your application in setup project, build the setup project.

Now create a batch file called uninstall.bat anywhere on your file system. In the batch file, write the statements given below:

@ECHO OFF 

This will set the commands to hide.

If at setup install path your application will create any directory at run time, then add the line given below in uninstall.bat file to remove the directory contents.

DEL DirectoryName /q

Add one command to clear your command prompt screen so that no one can come to know what your utility is doing behind the scenes.

CLS

Then to remove the directory itself, you will have to add one more line given below:

RMDIR DirectoryName

If your application is creating any file at runtime, you need to clear those files also at uninstallation. To achieve that, you can add the line given below:

XML
DEL FielName<.txt|.xml>

This will remove the file specified.

Now here is the main command which will uninstall your application, so I hope you have understood the command specified above, if not then please understand those commands first.

Now the command given below will remove all the files which were created by the installer and remove the application from the system.

C:\WINDOWS\system32\MsiExec.exe /Uninstall {455EAB09-DEC0-4EBF-8B4D-130004A7537D} 
/qr /li <LogFileName.txt>

C:\WINDOWS\system32\MsiExec.exe = Path of the MsiExec.exe file present in System folder, make sure it exists on the target machine or change the path accordingly.

/Uninstall = This is a command to tell the MsiExec.exe that you are performing uninstallation.

{455EAB09-DEC0-4EBF-8B4D-130004A7537D} = This is the most important part of the utility, this is the product code of your application.
You can get this product code from your application setup project in properties window of
your setup project. [Select project node in Solution Explorer and click F4 and copy the Product code.]

If you change the version of your application in setup project, then the product code is changed so whenever you change the version just copy the new product code and replace it in uninstall.bat file.

/qr = This is the command to tell MsiExec.exe that you will be performing uninstallation in GUI mode, but with less GUI, it means it will not ask you to repair your application.

There are multiple combinations to achieve different modes of uninstallation, like:

qn = No UI.
qb = Basic UI 
qf = Full UI [default] (It will ask you to either repair or uninstall your application. )

quiet = Quiet mode, no user interaction
passive = Unattended mode - progress bar only

Now if you want to create a log for your application uninstallation, then add one more command
given below:

/li <Log File Name e.g. MyAppLog.txt>

This will create a log file with a name of MyAppLog.txt.

And now uninstallation is done.

Now you have to include this batch file in the setup project where your primary output is going and you can create a shortcut of this batch file and put in programs menu if you are creating shortcuts for your application.

So when setup is installed, it will put uninstall.bat file in application installation root.

Points of Interest

I am saving my time again from uninstalling any application from addremove programs utility.

Any type of suggestions are always welcome and if you have any doubts or problems, please contact me.

Enjoy it.

Please visit to my web space for some more articles at http://www.naq.web1000.com.

Thank you.

License

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