Most of us will face a common problem, how to Uninstall our programs once it has been Installed. The below code snippet will give you the answer.
This uninstaller program needs an argument the ProductCode. It searches for the
ProductCode and remove/repair the application according to the choice X/I
provided respectively.
Build this as a seperate console application.
Add
this primary output into your setup.
Select your setup and press F4, from the
property window select the product code including {} braces let us say "{123abcd45678}"
is your product code. Now create short cut for your uninstall program.
Select that
shortcut then press F4, to get property window, paste this under Arguments attribute like
/u={123abcd45678}.
That's it.
Build your setup and deploy. After deployment click your uninstall icon to
uninstall your product.
// **** UNINSTALLER
using System;
using System.Diagnostics;
namespace Infosrama
{
public class Uninstaller
{
[STAThread]
static void Main(string[] args)
{
Uninstall();
}
public Uninstall()
{
string GUID;
string sPath;
ProcessStartInfo opInfo;
try
{
string[] arguments = Environment.GetCommandLineArgs();
foreach(string argument in arguments)
{
if ((argument.Split("=")[0].ToLower == "/u"))
{
GUID = argument.Split("=")[1];
sPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
opInfo = new ProcessStartInfo((sPath + "\\msiexec.exe"), ("/x " + GUID));
// use /x to provide remove option only.
// use /i to provide uninstall and repair.
Process.Start(opInfo);
}
}
}
catch(Exception ex)
{
MsgBox(ex.Message, MsgBoxStyle.Critical, "Exception");
}
finally
{
GUID = null;
sPath = null;
opInfo = null;
}
}
}
}