views:

548

answers:

3

I have created a VS setup project for my app. It install the app to user-defined location and creates several shortcuts in the start menu. It also creates an entry in Control Panel/Add or Remove Programs that can be used to uninstall the application.

I would like to know if there is a way to create a start menu entry (next to other entries created by setup) that could uninstall my app.

So far I found one solution but it's pain to use: I have created uninstall.bat file that I deploy in my app folder and I'm adding a shortcut to this file. Contents of the *.bat looks like this:

@echo off
msiexec /x {0B02B2AB-12C6-4548-BF90-F754372B0D36}

What I don't like about this solution is that every time I update a product code of my app (I'm doing that whenever I update my app version as VS suggests) I have to manually edit this file before building setup project and type correct new product code.

Does anyone knows a simpler way of adding uninstaller to the app?

+1  A: 

You can edit the .bat file to accept an argument.

@echo off
msiexec /x %1

In the setup project, where you define the shortcut, add the [ProductCode] property as the argument.

Magnus Johansson
I guess. But isn't there more elegant way of doing that? I would gladly remove the whole `bat` file and leave creating uninstaller entirely to setup project.
RaYell
In that case I would recommend you to use WiX instead.
Magnus Johansson
+1  A: 

I had this exact problem.

What I did was this:

  • provide the uninstall.bat file. This file gets installed unconditionally
  • provide a custom action in the installer, that rewrites the uninstall.bat file, and inserts the correct product code.


here's the script that runs as a custom action. It rewrites the uninstall.bat file, then deletes itself.

// CreateUninstaller.js
//
// Runs on installation, to create an uninstaller
// .cmd file in the application folder.  This makes it
// easy to uninstall. 
//
// Mon, 31 Aug 2009  05:13
//

var fso, ts;
var ForWriting= 2;
fso = new ActiveXObject("Scripting.FileSystemObject");

var parameters = Session.Property("CustomActionData").split("|"); 
var targetDir = parameters[0];
var productCode = parameters[1];

ts = fso.OpenTextFile(targetDir + "uninstall.cmd", ForWriting, true);


ts.WriteLine("@echo off");
ts.WriteLine("goto START");
ts.WriteLine("=======================================================");
ts.WriteLine(" Uninstall.cmd");
ts.WriteBlankLines(1);
ts.WriteLine(" This is part of MyProduct.");
ts.WriteBlankLines(1);
ts.WriteLine(" Run this to uninstall MyProduct");
ts.WriteBlankLines(1);
ts.WriteLine("=======================================================");
ts.WriteBlankLines(1);
ts.WriteLine(":START");
ts.WriteLine("@REM The uuid is the 'ProductCode' in the Visual Studio setup project");
ts.WriteLine("%windir%\\system32\\msiexec /x " + productCode);
ts.WriteBlankLines(1);
ts.Close();


// all done - try to delete myself.
try 
{
    var scriptName = targetDir + "createUninstaller.js";
    if (fso.FileExists(scriptName))
    {
        fso.DeleteFile(scriptName);
    }
}
catch (e2)
{
}

I think I could have done this with WiX, but I didn't want to learn it.

Cheeso
A: 

Another option is to call the msiexec to uninstall from the application itself, if a certain command line argument is given - see the example shown here for further detail: http://endofstream.com/creating-uninstaller-in-a-visual-studio-project/

By doing it this way, you will not be forced to see the command prompt when uninstalling :)

Best Regards /Peter

Peter