Kurubaran
Kurubaran

Reputation: 8902

How to uninstall MSI using its Product Code in c#

I know We can uninstall a windows application using its MSI by passing command arguments as follows,

Process p = new Process(); 
p.StartInfo.FileName = "msiexec.exe"; 
p.StartInfo.Arguments = "/x \"C:\\MyApplication.msi\"/qn"; 
p.Start(); 

But what i want to know is how can we uninstall the application without using MSI ? In the above scenario I should have the MSI in the specific location to uninstall this application, If i could unstaill using product code then I dont need to have the MSI in the target machine.

Upvotes: 7

Views: 15819

Answers (4)

Christopher Painter
Christopher Painter

Reputation: 55581

Along the lines of PhilmE's answer, Windows Installer XML (WiX) ships the Microsoft.Deployment.WindowsInstaller interop library as part of Deployment Tools Foundation (DTF). This skips the COM interop and encapsulates the Win32 API instead.

using Microsoft.Deployment.WindowsInstaller;

public static void Uninstall( string productCode)
{
    Installer.ConfigureProduct(productCode, 0, InstallState.Absent, @"REBOOT=""R"" /l*v uninstall.log");
}

Upvotes: 7

Philm
Philm

Reputation: 3674

Probably for your case, knowing the "/x" Parameter was sufficient. Two remarks on that: More secure is adding a "REBOOT=R" part to your commandline. And you can add a logfile path:

msiexec /x "..." /qn REBOOT=R /L*v "c:\mylogdir\mymsi.log"

Second, don't try to change anything to "the caching". You don't need even to understand it. If the cached package would be broken, a regular uninstallation is no longer possible, which could bring the computer in a "support needed" state.

Because your question was originally talking about C# .. You don't have to use msiexec for it:

a) Use the original C/C++ API with the function MsiInstallProduct() or MsiConfigureProduct(). MSDN ref: http://msdn.microsoft.com/en-us/library/windows/desktop/aa370315(v=vs.85).aspx

You have to use interop to use that in C#.

or b) Use the Windows Installer Object. For example, this related case was already answered here in stackoverflow: Programmatically installing MSI packages But using this function needs the physical package, also for uninstallation. With a slight indirection, here is the better code for uninstallation:

First, add a reference to COM object "Microsoft Windows Installer Object Library" to your project.

using WindowsInstaller;


public static class MyMsiLib
{
    public static void Uninstall(string productCode)
    {

         Type type = Type.GetTypeFromProgID("WindowsInstaller.Installer");
         Installer installer = (Installer)Activator.CreateInstance(type);
         installer.UILevel=msiUILevelNone;
         installer.ConfigureProduct(productCode, 0, msiInstallStateAbsent);
    }
}

The UILevel property before is set here hardcoded to determine the UI level silent as you seem to want. Same for other properties. See MSDN documentation e.g. mentioned in the link above.

Of course "real programmers" work with the original API instead of the "Installer Object" :-) But for small purposes it is sufficient. And easier.

Upvotes: 4

Ramesh Durai
Ramesh Durai

Reputation: 2686

According to MSDN, You can uninstall it using the product code:

msiexec.exe /x {your-product-code-guid}

When you use the product code, it uses the cached MSI from C:\WINDOWS\Installer.

Upvotes: 7

Bravo11
Bravo11

Reputation: 918

This command works on the command line:

msiexec /x {3A40307D-6DF2-4412-842F-B1D848043367} /quiet

I haven't tried it in C#, but replacing your arguments with the key shown above should work. You can find the GUID in the registry key for the app you are trying to uninstall.

Upvotes: 3

Related Questions