C#卸载软件

打开控制面板-程序和功能,里面能看到想要卸载的软件名称

根据DisplayName就能找到UninstallString

public static string GetProductGuid(string displayName)
{
    string productGuid = string.Empty;

    string bit32 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";

    RegistryKey localMachine = Registry.LocalMachine;
    RegistryKey unistall = localMachine.OpenSubKey(bit32, true);

    var subNames = unistall.GetSubKeyNames();

    foreach (string subkey in subNames)
    {
        RegistryKey product = unistall.OpenSubKey(subkey);
        try
        {
            if (product.GetValueNames().Any(n => n == "DisplayName") == true)
            {
                string tempDisplayName = product.GetValue("DisplayName").ToString();
                if (tempDisplayName == displayName && product.GetValueNames().Any(n => n == "UninstallString") == true)
                {
                    var unitstallStr = product.GetValue("UninstallString").ToString();
                    
                    //注意:如果不包含MsiExec,可以返回unitstallStr
                    if (unitstallStr.Contains("MsiExec.exe"))
                    {
                        string[] strs = unitstallStr.Split(new char[2] { '{', '}' });
                        productGuid = strs[1];
                        break;
                    }
                }
            }
        }
        catch
        {
            return string.Empty;
        }
    }

    return productGuid;
}

如果找不到,就要手动在注册表  HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall 下面找到DisplayName和卸载字符串

卸载

Process p = new Process();
p.StartInfo.FileName = "msiexec.exe";
p.StartInfo.Arguments = "/x {72D96FC3-7E2F-448B-86DA-4CB1C8882407}";

p.Start(); 

Process p = new Process();
p.StartInfo.FileName=@"C:\Program Files (x86)\InstallShield Installation Information\{05B77F59-5655-4E62-8139-BD9E400D8D15}\setup.exe" ;
p.StartInfo.Arguments=" -runfromtemp -l0x0409  -removeonly";
p.Start(); 

猜你喜欢

转载自www.cnblogs.com/code1992/p/12557105.html
今日推荐