Saturday, November 24, 2007

Create un-install setup shortcut and project

In order to create professional setup project using .Net setup and deployment projects, the you have to create your own way for un-installing the products using shortcut from start menu.

And you can accomplish that by:
1- creating a new project (console application) to your solution.
2- In program.cs class place the following code:
//**** Begin here
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace UninstallMyProject
{
///
/// Used to handle project custom uninstall from start menu or target directory
///

class Program
{
static void Main(string[] args)
{
string[] arguments = Environment.GetCommandLineArgs();

Process proc = new Process();
string path = Environment.GetFolderPath(Environment.SpecialFolder.System);

// Default product code must be changed if the product changed in setup project
string productCode = "{BBA4SSBD2-20B5-410C-8F23-C23E18806C7B}";

// if no arguments send then the uninstall called from exe itself ,, and
// for this we cant send the product code so assume the default one

if (arguments.Length == 1)
{
proc.StartInfo.FileName = string.Concat(path, "\\msiexec.exe");
proc.StartInfo.Arguments = string.Concat(" /i ", productCode);
proc.Start();
}
else
{
foreach (string argument in arguments)
{
string[] parameters = argument.Split('=');
if (parameters[0].ToLower() == "/u")
{
productCode = parameters[1];

proc.StartInfo.FileName = string.Concat(path, "\\msiexec.exe");
proc.StartInfo.Arguments = string.Concat(" /i ", productCode);
proc.Start();
}
}
}







}
}
}

//** Ends Here


3- Then you can just create Shortcut on your setup project, that point to this exe with the required product code as parameter. parameter in the shortcuts placed in argument property as: /u=[ProductCode]

Hope this information will be helpful, and if any help needed, just leave your comments.

Cheers,
Nasser