none
RegistryKey Class - Trouble with DeleteSubKey RRS feed

  • Question

  • I am attempting to use the RegistryKey Class to find and then delete a SubKey in the Uninstall section in Windows Registry.

    From another post I started, I was assisted and now able to locate the required SubKey, but now I am having trouble deleting it.

    I was getting various permission issues, and used code shown below, and thought all was OK, but now the DeleteSubKey returns an error saying the SubKey does not exist.

    I am running my application with administration rights.

    I am sure I have something simple that is wrong here which I am not getting quite right.

    Here is the current code I have:

    public static void DeleteUninstallRegKey(string DisplayName, string DisplayVersion)
            {         
                RegistryKey rootKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
                RegistryKey key = rootKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.Delete | System.Security.AccessControl.RegistryRights.ReadKey);
                if (key != null)
                {
                    string[] subkeys = key.GetSubKeyNames();
                    foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName, RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.Delete | System.Security.AccessControl.RegistryRights.ReadKey)))
                    {
                        string displayName = subkey.GetValue("DisplayName") as string;
                        if (displayName != null && displayName.Contains(DisplayName))
                        {
                            string displayVersion = subkey.GetValue("DisplayVersion") as string;
                            if (displayVersion != null && displayVersion.Contains(DisplayVersion))
                            {                            
                                subkey.DeleteSubKey(subkey.Name);
                                break;
                            }
                        }
                    }
                    key.Close();
                }            
            }

    Hoping someone may assist my lack of understanding here.

    Thanks.

    Tuesday, June 26, 2018 11:39 AM

All replies

  • It is unclear what your permission issues are I cannot speak to that. I assume it is the issue of trying to run an x86 app and not being able to see the x64 version of Uninstall? This code works for me (in either x86 or x64 mode).

    public static void DeleteUninstallRegKey ( string displayName, string displayVersion )
    {
        using (var hive = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
        using (var root = hive.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall", true))             
        {
            //Enumerate the children
            var children = root.GetSubKeyNames();
            foreach (var child in children)
            {
                //Check for a match
                var isMatch = false;
                using (var key = root.OpenSubKey(child))
                {
                    if (String.Compare(key.GetValue("DisplayName")?.ToString(), displayName, true) == 0 &&
                        String.Compare(key.GetValue("DisplayVersion")?.ToString(), displayVersion, true) == 0)
                        isMatch = true;
                };
    
                if (isMatch)
                    root.DeleteSubKeyTree(child);
            };
        };
    }      

    Could probably clean this up with some extension methods and maybe even LINQ but for performance purposes this should be fast enough.



    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, June 26, 2018 2:56 PM
    Moderator