Click here to Skip to main content
15,891,951 members
Articles / Operating Systems / Windows
Article

Add an uninstall start menu item to your .NET deployment project

Rate me:
Please Sign up or sign in to vote.
4.66/5 (26 votes)
21 Aug 2005CPOL 265K   77   54
The simple way to add an uninstall menu item to your .NET deployment project

Introduction

It's super easy to add this to your deployment project.

  1. Select your deployment project and go to the file system editor, user programs menu.
  2. Add an additional shortcut to your primary output project and name it Uninstall Application.
  3. Set the Arguments property to /u=[ProductCode].
  4. Add the following code to your project's Main() sub or startup form's New() sub just before the call to InitializeComponent().
    VB
    Dim arguments As String() = Environment.GetCommandLineArgs()
    Dim argument As String
    For Each argument In arguments
        If argument.Split("=")(0).ToLower = "/u" Then
            Dim guid As String = argument.Split("=")(1)
            Dim path As String = _
               Environment.GetFolderPath(Environment.SpecialFolder.System)
            Dim si As New ProcessStartInfo(path & _
                      "\msiexec.exe", "/i " & guid)
            Process.Start(si)
            Close()
            Application.Exit()
            End
        End If
    Next

That's is! The 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 before exiting.

The installer is run in repair/remove mode for your application. The user is allowed to select repair or remove, and continue. If you want the product to remove only, replace the "/i " with "/x ".

License

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


Written By
Systems Engineer Mark J Means Consulting
United States United States
I have been a software consultant since 1985 working on everything from the Commodore VIC-20 & RadioShack CoCo games to 8051 Embedded USB Microcontrollers to Windows Vista database applications. I have written over a half million lines of code since 2004. Please see my DataConnectionDialog control at http://mjmeans.com/dcd.aspx.

Comments and Discussions

 
QuestionThanks Pin
Kashif Alvi25-Jun-13 11:02
Kashif Alvi25-Jun-13 11:02 
QuestionWorked but getting an error in some machines Pin
Vaidehi P16-May-11 20:21
Vaidehi P16-May-11 20:21 
AnswerRe: Worked but getting an error in some machines Pin
mjmeans17-May-11 11:00
mjmeans17-May-11 11:00 
GeneralVerifying The Link... [modified] Pin
jp2code15-Sep-10 12:04
professionaljp2code15-Sep-10 12:04 
GeneralCondensed C# Code Pin
Geo24227-May-10 13:01
Geo24227-May-10 13:01 
GeneralRe: Condensed C# Code Pin
satheesh.palanisamy28-Sep-12 3:10
satheesh.palanisamy28-Sep-12 3:10 
GeneralCode in C# Pin
Nitin S3-Dec-09 15:53
professionalNitin S3-Dec-09 15:53 
QuestionWhere should I put these codes? Pin
qiux4-Sep-09 0:51
qiux4-Sep-09 0:51 
AnswerRe: Where should I put these codes? Pin
mjmeans5-Sep-09 23:01
mjmeans5-Sep-09 23:01 
QuestionHow to Show Product Name while confirming the Unistallation? Pin
Upendra Jagdale19-Aug-09 20:45
professionalUpendra Jagdale19-Aug-09 20:45 
AnswerRe: How to Show Product Name while confirming the Unistallation? Pin
mjmeans20-Aug-09 7:22
mjmeans20-Aug-09 7:22 
AnswerRe: How to Show Product Name while confirming the Unistallation? Pin
stratokazik17-Aug-10 19:45
stratokazik17-Aug-10 19:45 
GeneralThank you Pin
Sintre8-Jun-09 11:14
Sintre8-Jun-09 11:14 
GeneralOne Problem [modified] Pin
thirstyDev26-Nov-08 18:43
thirstyDev26-Nov-08 18:43 
GeneralRe: One Problem Pin
mjmeans27-Nov-08 7:37
mjmeans27-Nov-08 7:37 
GeneralRe: One Problem Pin
thirstyDev27-Nov-08 17:46
thirstyDev27-Nov-08 17:46 
GeneralEven easier solution PinPopular
Vyacheslav Trubarov3-Nov-07 14:28
Vyacheslav Trubarov3-Nov-07 14:28 
I've found even easier solution for uninstall menu item:

1. Create new shortcut in you system (NOT in VS Project, just in Windows).
2. In the location field type "msiexec".
3. Name the shortcut, for example, "Uninstall".
4. Now open its properties. Windows automatically used relative path to target: "%windir%\system32\msiexec.exe".
5. Add to the target "/x {ProductCode}". Here, you must specify the REAL ProductCode of you application (see it in your Deployment Project Properties -> ProductCode). The final target should look, for example, like this: "%windir%\system32\msiexec.exe /x {1CAD0188-B15F-4C31-8146-A267FDA507CB}".
6. Now you need to add this file to your Deployment Project. But here was the problem - u can't add shortcuts as files to your project. To bypass this restriction, you need just to rename your shortcut to have different extension. You can't do this in Windows Explorer, so use any file manager (Far, Total Commander, NDN, etc). Find and rename the shortcut we've created earlier from "Uninstall.lnk" to, for example, "Uninstall.lnk.foo".
7. Add this renamed shortcut to your Deployment Project (Add -> File...).
8. Open Properties of the added file and set TargetName back to initial name Smile | :) . (e.g. "Uninstall.lnk").
9. Move this file inside the Deployment Project to the folder your shortcuts would deploy.

That's all, now you will have the shortcut to uninstall your program without any additional files in your project (e.g. .bat files, etc)!

This could sound a bit complicated - but I've given so many steps only to explain the process very detailed. Actually, there is not so much to do. The only disadvantage of this method is, that you need to change ProductCode in the uninstall shortcut manually every time you change it in your deployment project.
This solution worked just well for me (i tested it on WinXP, but other versions could work as well, just maybe with some differences in the system pathes), so I think it could suit others as well Smile | :) .

NewsRe: Even easier solution Pin
Bilal Haider13-Mar-08 8:46
professionalBilal Haider13-Mar-08 8:46 
GeneralNo Code Solution VS2005 Pin
code@controlbox.co.uk9-Aug-07 5:52
code@controlbox.co.uk9-Aug-07 5:52 
GeneralRe: No Code Solution VS2005 Pin
mjmeans9-Aug-07 7:26
mjmeans9-Aug-07 7:26 
GeneralRe: No Code Solution VS2005 Pin
cedear19-May-08 5:50
cedear19-May-08 5:50 
GeneralRe: No Code Solution VS2005 Pin
nnans29-Oct-08 0:07
nnans29-Oct-08 0:07 
GeneralRe: No Code Solution VS2005 Pin
mjmeans29-Oct-08 6:16
mjmeans29-Oct-08 6:16 
GeneralRe: No Code Solution VS2005 Pin
nnans29-Oct-08 17:07
nnans29-Oct-08 17:07 
GeneralRe: No Code Solution VS2005 Pin
mjmeans30-Oct-08 2:08
mjmeans30-Oct-08 2:08 

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.