Uninstalling program

Duplicates: Welcome to Stackoverflow. Just to mention to you that I see this question asked in at least 3 different flavors. We will have to close some of your questions since the duplication scatters replies and can waste a lot of time if people answer the (seemingly) unanswered duplicates.

In short: please don’t post the same question several times. Here are the other questions:


C#: Using C# for this can be clunky – no matter how you do it. I would not push a command line to msiexec.exe, but go directly via the MSI API. This API can be accessed via Win32 functions or COM automation.

Uninstall Appraches for MSI: For your reference, there is a myriad of ways to kick of an MSI
uninstall:
Uninstalling an MSI file from the command line without using msiexec.

Section 14 from the link above shows how to uninstall using C++ – if that is an option. However:, there are changes in the Visual Studio 2017 templates again, so it might need a tune-up to work “out-of-the-box”.

However, I would use the MSI API – as already stated – and I would recommend you go via the native Win32 functions and that you use DTF (Deployment Tools Foundation) which is part of the WiX toolkit. It is a .NET wrapper for the MSI API – which will save you a lot of boilerplate code, at the expense of having to deploy the DTF DLL: Microsoft.Deployment.WindowsInstaller.dll along with your product. I do not know if this is acceptable. I have code that does not depend on DTF if need be, but it is much longer.

Mock-up C# Sample. Project reference to Microsoft.Deployment.WindowsInstaller.dll needed. Then try the below code in a fresh C# .NET project. You can get that DLL by installing the WiX toolkit – the open source toolkit to create MSI files. After installation check in %ProgramFiles(x86)%\WiX Toolset v3.11\bin (adjust for WiX version – current as of September 2018).

Installer GUI: Important note first: the setup’s UI level is set via the Installer.SetInternalUI function. If you run in silent mode, then you need to run the executable elevated for the uninstall to work properly, or an access exception occurs. When you run in Full GUI mode, you need to elevate the install yourself – provided you have the rights to do so.

Run Elevated: How to check for admin rights: Check if the current user is administrator.

using System;
using Microsoft.Deployment.WindowsInstaller;

namespace UninstallMsiViaDTF
{
    class Program
    {
        static void Main(string[] args)
        {
            // Update this name to search for your product. This sample searches for "Orca"
            var productcode = FindProductCode("orca");

            try
            {
                if (String.IsNullOrEmpty(productcode)) { throw new ArgumentNullException("productcode"); }

                // Note: Setting InstallUIOptions to silent will fail uninstall if uninstall requires elevation since UAC prompt then does not show up 
                Installer.SetInternalUI(InstallUIOptions.Full); // Set MSI GUI level (run this function elevated for silent mode)
                Installer.ConfigureProduct(productcode, 0, InstallState.Absent, "REBOOT=\"ReallySuppress\"");

                // Check: Installer.RebootInitiated and Installer.RebootRequired;
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }

            Console.ReadLine(); // Keep console open
        }

        // Find product code for product name. First match found wins
        static string FindProductCode(string productname)
        {
            var productcode = String.Empty;
            var productname = productname.ToLower();
            foreach (ProductInstallation product in ProductInstallation.AllProducts)
            {
                if (product.ProductName.ToLower().Contains(productname))
                {
                    productcode = product.ProductCode;
                    break;
                }
            }

            return productcode;
        }
    }
}

Leave a Comment