使用 C# 以编程方式卸载软件

Programmatically Uninstall a Software using C#

我想用我的代码卸载一个软件,我已经尝试过wmic方法来执行卸载,但它无法在系统中找到我的软件。是否可以在不使用 msi 文件或任何安装文件的情况下进行卸载。我找到了这段代码,但它不起作用---

public string GetUninstallCommandFor(string productDisplayName)
{
    RegistryKey localMachine = Registry.LocalMachine;
    string productsRoot = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products";
    RegistryKey products = localMachine.OpenSubKey(productsRoot);
    string[] productFolders = products.GetSubKeyNames();

    foreach (string p in productFolders)
    {
        RegistryKey installProperties = products.OpenSubKey(p + @"\InstallProperties");
        if (installProperties != null)
        {
            string displayName = (string)installProperties.GetValue("DisplayName");
            if ((displayName != null) && (displayName.Contains(productDisplayName)))
            {
                string uninstallCommand = (string)installProperties.GetValue("UninstallString");
                return uninstallCommand;
            }
        }
    }
    return "";
}

最可靠的方法是以编程方式执行以下 shell 命令:

msiexec.exe /x {PRODUCT-GUID}

如果您制作了原始 MSI,您将可以访问您的 PRODUCT-GUID,这就是您所需要的。不需要实际的 MSI 文件,因为 Windows 正是为此目的保存了一份副本。

仅供参考:

Windows ® Installer. V 5.0.14393.0 

msiexec /Option <Required Parameter> [Optional Parameter]

Install Options
    </package | /i> <Product.msi>
        Installs or configures a product
    /a <Product.msi>
        Administrative install - Installs a product on the network
    /j<u|m> <Product.msi> [/t <Transform List>] [/g <Language ID>]
        Advertises a product - m to all users, u to current user
    </uninstall | /x> <Product.msi | ProductCode>
        Uninstalls the product
Display Options
    /quiet
        Quiet mode, no user interaction
    /passive
        Unattended mode - progress bar only
    /q[n|b|r|f]
        Sets user interface level
        n - No UI
        b - Basic UI
        r - Reduced UI
        f - Full UI (default)
    /help
        Help information
Restart Options
    /norestart
        Do not restart after the installation is complete
    /promptrestart
        Prompts the user for restart if necessary
    /forcerestart
        Always restart the computer after installation
Logging Options
    /l[i|w|e|a|r|u|c|m|o|p|v|x|+|!|*] <LogFile>
        i - Status messages
        w - Nonfatal warnings
        e - All error messages
        a - Start up of actions
        r - Action-specific records
        u - User requests
        c - Initial UI parameters
        m - Out-of-memory or fatal exit information
        o - Out-of-disk-space messages
        p - Terminal properties
        v - Verbose output
        x - Extra debugging information
        + - Append to existing log file
        ! - Flush each line to the log
        * - Log all information, except for v and x options
    /log <LogFile>
        Equivalent of /l* <LogFile>
Update Options
    /update <Update1.msp>[;Update2.msp]
        Applies update(s)
    /uninstall <PatchCodeGuid>[;Update2.msp] /package <Product.msi | ProductCode>
        Remove update(s) for a product
Repair Options
    /f[p|e|c|m|s|o|d|a|u|v] <Product.msi | ProductCode>
        Repairs a product
        p - only if file is missing
        o - if file is missing or an older version is installed (default)
        e - if file is missing or an equal or older version is installed
        d - if file is missing or a different version is installed
        c - if file is missing or checksum does not match the calculated value
        a - forces all files to be reinstalled
        u - all required user-specific registry entries (default)
        m - all required computer-specific registry entries (default)
        s - all existing shortcuts (default)
        v - runs from source and recaches local package
Setting Public Properties
    [PROPERTY=PropertyValue]

试试这个

我们使用以下格式得到一个 ManagementObject 属性:

列出已安装应用程序的完整代码块:

using System.Management
private List<string> ListPrograms()
{
    List<string> programs = new List<string>();

    try
    {
        ManagementObjectSearcher mos = 
          new ManagementObjectSearcher("SELECT * FROM Win32_Product");
        foreach (ManagementObject mo in mos.Get())
        {
            try
            {
                //more properties:
                //http://msdn.microsoft.com/en-us/library/windows/desktop/aa394378(v=vs.85).aspx
                programs.Add(mo["Name"].ToString());

            }
            catch (Exception ex)
            {
                //this program may not have a name property
            }
        }

        return programs;

    }
    catch (Exception ex)
    {
        return programs;
    }
}

现在我们有了已安装应用程序的列表,我们应该能够将 [Name] 属性 传递给我们的卸载方法。

we now need to Invoke the Win32_Product method to “Uninstall”

这里是卸载应用的全过程,等你看完再详细说。

  private bool UninstallProgram(string ProgramName)
    {
        try
        {
            ManagementObjectSearcher mos = new ManagementObjectSearcher(
              "SELECT * FROM Win32_Product WHERE Name = '" + ProgramName + "'");
            foreach (ManagementObject mo in mos.Get())
            {
                try
                {
                    if (mo["Name"].ToString() == ProgramName)
                    {
                        object hr = mo.InvokeMethod("Uninstall", null);
                        return (bool)hr;
                    }
                }
                catch (Exception ex)
                {
                    //this program may not have a name property, so an exception will be thrown
                }
            }

            //was not found...
            return false;

        }
        catch (Exception ex)
        {
            return false;
        }
    }

这是一个老问题,可能来不及回答,但无论如何这是我的代码

 public static void UninstallApplication(string uninstallString)
    {
        if (string.IsNullOrEmpty(uninstallString))
        {
            throw new ArgumentNullException(nameof(uninstallString));
        }

        ProcessStartInfo startInfo = new ProcessStartInfo();

        int indexofexe = uninstallString.IndexOf(".exe");
        //Check for executable existence 
        if (indexofexe > 0)
        {
            uninstallString = uninstallString.Replace(@"""", string.Empty);

            //Get exe path 
            string uninstallerPath = uninstallString.Substring(0, indexofexe + 4);
            startInfo.FileName = uninstallerPath;

            //Check for arguments
            if (uninstallerPath.Length != uninstallString.Length)
            {
                string args = uninstallString.Substring(uninstallerPath.Length);
                if (!string.IsNullOrEmpty(args))
                {

                    /*If not set to false You will get InvalidOperationException :
                     *The Process object must have the UseShellExecute property set to false in order to use environment variables.*/
                    startInfo.UseShellExecute = false;

                    startInfo.Arguments = args;
                }
            }
        }
        //Not tested 
        else
        {
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "/c " + uninstallString;
        }

        //Start the process
        Process.Start(startInfo).WaitForExit();
    }
  

这适用于我的大部分应用程序