C#卸载软件

2021-03-13 06:30

阅读:369

标签:mat   roc   rom   contain   根据   img   com   art   isp   

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

技术图片

 

根据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";
//用 /i 替换 /x 就不会弹出提示框 “您确定要卸载此产品吗?”
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(); 

 

C#卸载软件

标签:mat   roc   rom   contain   根据   img   com   art   isp   

原文地址:https://www.cnblogs.com/code1992/p/12557105.html


评论


亲,登录后才可以留言!