MSI installer option- uninstalling an application

2019-07-31 17:36发布

This question is an exact duplicate of:

If I run the code below I'm pretty sure I'm supposed to get the Product Name and GUID (ex. App Path | {xxx}) for the application. But I'm only getting the path and no GUID is shown. Can someone help me?

// search in: LocalMachine_64
key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
foreach (String keyName in key.GetSubKeyNames())
{
    RegistryKey subkey = key.OpenSubKey(keyName);
    displayName = Convert.ToString(subkey.GetValue("DisplayName"));
    uninstlString = Convert.ToString(subkey.GetValue("UninstallString"));

    if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase))
    {
        Console.WriteLine(subkey.GetValue("UninstallString"));
        //string prdctId = uninstlString.Substring((uninstlString.IndexOf("{")));
        string prdctId = uninstlString.Substring(12);
        uninstallProcess.StartInfo.FileName = "MsiExec.exe";
        uninstallProcess.StartInfo.Arguments = " /x " + prdctId + " /quiet /norestart";
        uninstallProcess.StartInfo.UseShellExecute = true;
        uninstallProcess.Start();
        uninstallProcess.WaitForExit();
        break;
        //Console.WriteLine(subkey.GetValue("UninstallString"));
    }
}

This is the image that I got running the code This is the image that I got running the code

1条回答
一纸荒年 Trace。
2楼-- · 2019-07-31 18:17

I believe the UninstallString value is what gets executed when uninstalling an application via Add/Remove Programs. As your console output shows, it's the path to an executable.

The way you are retrieving the product ID...

string prdctId = uninstlString.Substring(12);

...therefore, is incorrect because you are taking a partial path. What you need to pass to MsiExec.exe /x is the product code, which is the registry key name itself, i.e....

string prdctId = keyName;

If you were invoking that command line from Command Prompt I'm pretty sure the curly brackets would necessitate putting quotes around the product code; I'm not sure if you'll need to do so when invoking the executable directly, but it can't hurt...

uninstallProcess.StartInfo.Arguments = " /x \"" + prdctId + "\" /quiet /norestart";
查看更多
登录 后发表回答