How to uninstall MSI using its Product Code in c#

牧云@^-^@ 提交于 2019-11-28 13:56:41

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.

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");
}
Philm

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.

Bravo11

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!