---
title: "Uninstall utility in C#.NET"  
description: "Here I’m going to demonstrate how to make a small demo for uninstall utility through C#.Net."  
author: "Vijay Shukla"  
published: 2013-10-10  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1379/uninstall-utility-in-c-sharp-dot-net  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# Uninstall utility in C#.NET

## 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](https://yourviews.mindstick.com/story/1788/things-to-ensure-your-construction-project-is-successful) on [visual studio](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available) 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](https://yourviews.mindstick.com/view/83909/why-mindstick-platform-is-so-important-for-our-product-promotions) Information

[DllImport("msi.dll")]

static extern Int32 MsiGetProductInfo(string product, string [property](https://www.mindstick.com/articles/198559/an-ownership-of-property-in-hua-hin), [Out] [StringBuilder](https://www.mindstick.com/blog/99/stringbuilder-class-in-c-sharp) 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](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net) (**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](https://www.mindstick.com/forum/424/how-to-call-form_paint-event-on-button-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](https://www.mindstick.com/articles/61/exception-handling-in-c-sharp) 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](https://answers.mindstick.com/qa/95487/how-do-you-troubleshoot-when-your-application-link-is-not-responding).

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: -

![Uninstall utility in C#.NET](https://www.mindstick.com/mindstickarticle/9f8a9cf0-b768-4867-88b1-580f0afc802f/images/83757a6e-58f0-48a5-a32c-04cfe93167bb.png)

In above screen select any product and click to uninstall button then it uninstall wizard will appear.

---

Original Source: https://www.mindstick.com/articles/1379/uninstall-utility-in-c-sharp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
