Click here to Skip to main content
15,892,809 members
Articles / Operating Systems / Windows

An Uninstaller To Uninstall Any .NET Application

Rate me:
Please Sign up or sign in to vote.
3.05/5 (7 votes)
2 Aug 2007CPOL3 min read 62.4K   1.5K   29   4
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)


Written By
Web Developer
India India
I m a Graduate in Computer Science.
My hobies are to scratch new things from it's bottom and develop new thigs which can be useful for me and others.

So i got the Software developement the best stream where u can help others and u will be pampered with everything.

When i watch someone happy then i feel like that person is sharing my happiness and u know if someone is happy with u'r matters then that is the true person beyond everything for u.

So plz help others but first try to help u'r self then only u will enjoy helping others.
Good Luck.....

Please visit to my web space for some more articles,




http://www.FamilyCode.com




Thank you.

Comments and Discussions

 
GeneralCompleting This Solution - A Better Version Pin
kelvin199711-Nov-09 3:59
kelvin199711-Nov-09 3:59 
Hi,

There is an option to hide the black command line window.

In your solution, create a new WinForm project. Call it "Uninstaller"

Open Form1.
To make the form look better:
1.
In the Form properties...
FormBorderStyle: None
Size: 200, 40

2.
Dock fill a Panel in the Form.
Panel borderStyle property choose Single.

3.
put a Label the Panel. Label.Text = "Uninstalling ..."

4.
Code behind:
Public Class UninstallForm
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UninstallAsync()
    End Sub
#Region " Uninstall "
    Private WithEvents _UninstallWorker As New System.ComponentModel.BackgroundWorker
    Private Sub UninstallAsync()
        _UninstallWorker.RunWorkerAsync()
    End Sub
    Private Sub UninstallVersion(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles _UninstallWorker.DoWork
        Try
            'hold your uninstalling screen up for awhile
            System.Threading.Thread.Sleep(2000) 
            'uninstall your program
            Dim process As System.Diagnostics.Process = System.Diagnostics.Process.Start("msiexec.exe", "/x {########-####-####-####-##############}")
        Catch ex As Exception
        End Try
    End Sub
    Private Sub UninstallCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles _UninstallWorker.RunWorkerCompleted
        Try
            'shut itself down, so it won't get in the way when uninstalling
            Me.Close()
        Catch ex As Exception
        End Try
    End Sub
#End Region
End Class


Now compile it, include this assembly, uninstall.exe, into the Setup project output. Done.

With a little bit of imagination, nothing is impossible!

Have fun Smile | :) Rose | [Rose]
GeneralRe: Completing This Solution - A Better Version Pin
SohelElite17-Nov-09 20:43
SohelElite17-Nov-09 20:43 
GeneralGreate article Pin
Pankaj - Joshi22-Oct-07 0:36
Pankaj - Joshi22-Oct-07 0:36 
GeneralRe: Greate article Pin
Hungry Mind22-Oct-07 0:55
Hungry Mind22-Oct-07 0:55 

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.