Uninstall utility in C#.NET
Here I’m going to demonstrate how to make a small demo for uninstall utility through C#.Net.
1. Create a project on visual studio with appropriate name.
2. Create a class with DataBind name
public class DataBind
{
public string pName { get; set; }
public string pKey { get; set; }
}
3. Import the DLL [DllImport("msi.dll")]which is helpful for getting the installed product Information
[DllImport("msi.dll")]
static extern Int32 MsiGetProductInfo(string product, string property, [Out] StringBuilder valueBuf, ref Int32 len);
[DllImport("msi.dll", SetLastError = true)]
static extern int MsiEnumProducts(int iProductIndex, StringBuilder lpProductBuf);
4. Create a dataGridBind()method, this method is bind the all installed application list in the DataGridView (dgInstalld). This method is called on Form Load event.
private void dataGridBind()
{
List<DataBind> lstDataBind = new List<DataBind>();
StringBuilder sbProductCode = new StringBuilder(39);
int iIdx = 0;
while (0 == MsiEnumProducts(iIdx++, sbProductCode))
{
Int32 productNameLen = 512;
StringBuilder sbProductName = new StringBuilder(productNameLen);
MsiGetProductInfo(sbProductCode.ToString(), "ProductName", sbProductName, ref productNameLen);
lstDataBind.Add(new DataBind { pKey = sbProductCode.ToString(), pName = sbProductName.ToString() });
}
dgInstalld.DataSource = lstDataBind;
dgInstalld.Columns[0].Width = 332;
dgInstalld.Columns[1].Width = 248;
dgInstalld.Columns[0].HeaderText = "Installed Application";
dgInstalld.Columns[1].HeaderText = "Product Key";
}
5. Handle the DataGridView Cell content click event for select the product for uninstall the application
private void dgInstalld_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
btnUninstall.Enabled = true;
var cellindex = dgInstalld.SelectedCells[0].RowIndex;
var cellcollection = dgInstalld.Rows[cellindex].Cells[1];
pKeyValue = cellcollection.EditedFormattedValue.ToString();
}
6. After that create a method which works for uninstalled the selected application in DataGridView (dgInstalld) to on the basis of installed product Key.
public void Uninstall(string productCode)
{
try
{
Type type = Type.GetTypeFromProgID("WindowsInstaller.Installer");
Installer installer = (Installer)Activator.CreateInstance(type);
installer.ConfigureProduct(productCode, 0, MsiInstallState.msiInstallStateAbsent);
pKeyValue = null;
btnUninstall.Enabled = false;
dataGridBind();
}
catch (Exception ex) { }
}
7. Above Uninstall method is called form uninstall button on click event and it is ask a for uninstalling or not after your confirmation it will uninstall your application.
private void btnUninstall_Click(object sender, EventArgs e)
{
frmConfirmation frmConfirm = new frmConfirmation();
if (pKeyValue != null)
{
DialogResult result = MessageBox.Show("Do you really want to uninstall the product?", "Confirm product deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if(result==DialogResult.Yes)
Uninstall(pKeyValue);
}
else
btnUninstall.Enabled = false;
}
Full Code: -
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
btnUninstall.Enabled = false;
}
public class DataBind
{
public string pName { get; set; }
public string pKey { get; set; }
}
string pKeyValue = null;
[DllImport("msi.dll", CharSet = CharSet.Unicode)]
static extern Int32 MsiGetProductInfo(string product, string property,
[Out] StringBuilder valueBuf, ref Int32 len);
[DllImport("msi.dll", SetLastError = true)]
static extern int MsiEnumProducts(int iProductIndex,
StringBuilder lpProductBuf);
private void Form1_Load(object sender, EventArgs e)
{
dataGridBind();
}
private void btnUninstall_Click(object sender, EventArgs e)
{
frmConfirmation frmConfirm = new frmConfirmation();
if (pKeyValue != null)
{
DialogResult result = MessageBox.Show("Do you really want to uninstall the product?", "Confirm product deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if(result==DialogResult.Yes)
Uninstall(pKeyValue);
}
else
btnUninstall.Enabled = false;
}
private void dgInstalld_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
btnUninstall.Enabled = true;
var cellindex = dgInstalld.SelectedCells[0].RowIndex;
var cellcollection = dgInstalld.Rows[cellindex].Cells[1];
pKeyValue = cellcollection.EditedFormattedValue.ToString();
}
public void Uninstall(string productCode)
{
try
{
Type type = Type.GetTypeFromProgID("WindowsInstaller.Installer");
Installer installer = (Installer)Activator.CreateInstance(type);
installer.ConfigureProduct(productCode, 0, MsiInstallState.msiInstallStateAbsent);
pKeyValue = null;
btnUninstall.Enabled = false;
dataGridBind();
}
catch (Exception ex) { }
}
private void dataGridBind()
{
List<DataBind> lstDataBind = new List<DataBind>();
StringBuilder sbProductCode = new StringBuilder(39);
int iIdx = 0;
while (0 == MsiEnumProducts(iIdx++, sbProductCode))
{
Int32 productNameLen = 512;
StringBuilder sbProductName = new StringBuilder(productNameLen);
MsiGetProductInfo(sbProductCode.ToString(), "ProductName", sbProductName, ref productNameLen);
lstDataBind.Add(new DataBind { pKey = sbProductCode.ToString(), pName = sbProductName.ToString() });
}
dgInstalld.DataSource = lstDataBind;
dgInstalld.Columns[0].Width = 332;
dgInstalld.Columns[1].Width = 248;
dgInstalld.Columns[0].HeaderText = "Installed Application";
dgInstalld.Columns[1].HeaderText = "Product Key";
}
}
Output: -

In above screen select any product and click to uninstall button then it uninstall wizard will appear.
Leave Comment