1

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

Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164
BongJae
  • 49
  • 5
  • It doesn't matter how many times you *ask*, you're still going to get similar answers. `UninstallString` is *what you run to uninstall that application*. You cannot/should not pull it apart, try to process it, etc. – Damien_The_Unbeliever Sep 11 '18 at 17:12
  • What *is* clear from this succession of questions is that *you* (personally, your team/department/unit/company) don't *own* the applications that you're seeking to uninstall. (If you did, you wouldn't be ferretting through the registry to try to find uninstall information for them). The fact that you want to *silently uninstall* applications that you do not own makes me suspect your *motives* seriously. If there's a *legitimate* reason for doing this, please **edit** one of your existing questions and make that use case clear. – Damien_The_Unbeliever Sep 11 '18 at 17:23

1 Answers1

1

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";
Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
  • Sorry but then how do I get the keyName...? – BongJae Sep 11 '18 at 01:43
  • You already have `keyName`. It's your `foreach` loop iteration variable. – Lance U. Matthews Sep 11 '18 at 01:44
  • oh right.. then is the UninstallString unneccessary? – BongJae Sep 11 '18 at 01:46
  • Yes and no. It's not needed for your code to invoke `MsiExec.exe` the way you are, however you could execute the command line stored in `UninstallString` instead of using `MsiExec.exe` at all, though you're at the mercy of what that uninstaller happens to provide instead of the consistent interface of `MsiExec.exe`. – Lance U. Matthews Sep 11 '18 at 01:50
  • I've tried it...Still not uninstalling : – BongJae Sep 11 '18 at 01:50
  • Is it still just showing that `Windows Installer` usage dialog? What is the value of `uninstallProcess.StartInfo.Arguments`? – Lance U. Matthews Sep 11 '18 at 01:55
  • I gave the same value you told me above... is just nothing happens at all – BongJae Sep 11 '18 at 01:56
  • With `/quiet` you will see no output whether it succeeds or fails, which is to be expected. So, I would recommend removing `/quiet` until you figure out what's wrong, and also check the value of `uninstallProcess.ExitCode`. Are you testing this code as an elevated user? If not, that would cause the uninstall to fail. – Lance U. Matthews Sep 11 '18 at 02:04
  • oh actually I've got it working! just ran the uninstallstring...But now it says I need permission.. – BongJae Sep 11 '18 at 02:07
  • Right, since this is a machine-level installation `uninstallProcess` will need to be elevated whether you are executing `MsiExec.exe` or `UninstallString`. – Lance U. Matthews Sep 11 '18 at 02:10
  • Srry but do you know why some application give just the directory path and some gives msiexec.exe /x {xxxxx}??? – BongJae Sep 11 '18 at 02:38
  • `UninstallString` is an arbitrary command, so it's up to the application what value it uses. If it's a Windows Installer application it will likely use `MsiExec.exe`, if not it will have to use its own uninstaller executable. – Lance U. Matthews Sep 11 '18 at 02:41