Delete a Registry key using C#

倾然丶 夕夏残阳落幕 提交于 2021-01-28 12:09:47

问题


I am trying to delete a Registry key like this:

RegistryKey oRegistryKey = Registry.CurrentUser.OpenSubKey(
    "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts", true);

oRegistryKey.DeleteSubKeyTree(".");

But that is giving me an exception:

Cannot delete a subkey tree because the subkey does not exist

If I change DeleteSubKeyTree to DeleteSubKey, I receive a different exception:

Registry key has subkeys and recursive removes are not supported by this method


回答1:


Try this:

string str = @"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts";
string[] strSplit = strLocal.Split('\\');
            using (RegistryKey oRegistryKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts", true))
            {
                RegistryKey hdr = oRegistryKey.OpenSubKey(strSplit[strSplit.Length-2], true);
                foreach (String key in hdr.GetSubKeyNames())
                    hdr.DeleteSubKey(key);
                hdr.Close();
                oRegistryKey.DeleteSubKeyTree(strSplit[strSplit.Length - 2]);
            }

Also check: Registry in .NET: DeleteSubKeyTree says the subkey does not exists, but hey, it does!




回答2:


The approach outlined in this answer is needlessly complex because DeleteSubKeyTree is recursive. From its documentation on MSDN:

Deletes a subkey and any child subkeys recursively.

So, if your goal is to delete the user's FileExts key, do this:

string explorerKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Explorer";

using (RegistryKey explorerKey =
    Registry.CurrentUser.OpenSubKey(explorerKeyPath, writable: true))
{
    if (explorerKey != null)
    {
        explorerKey.DeleteSubKeyTree("FileExts");
    }
}

However, are you sure that you really want to delete a user's FileExts key? I believe that most would consider doing so would be unreasonably destructive and reckless. A more common scenario would be deleting a single file extension key (e.g., .hdr) from the FileExts key.

Finally, note that DeleteSubKeyTree is overloaded. Here is the signature of the second version of this method:

public void DeleteSubKeyTree(
    string subkey,
    bool throwOnMissingSubKey
)

With this version, if subkey does not exist and throwOnMissingSubKey is false, DeleteSubKeyTree will simply return without making any changes to the Registry.




回答3:


First you need to check the registry key:

using (RegistryKey Key = Registry.CurrentUser.OpenSubKey(@"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts"))
    if (Key != null)
    {    
       key.DeleteValue("."); //delete if exist
    }
    else
    {
        MessageBox.Show("key not found");
    }



回答4:


This is pretty straight forward. Close your registry. Reopen it. U will not find "." name key their. Because its already deleted in your first attempt.

Create a new Key with name "." and try to delete with the same code (twice). you will find your answer




回答5:


This will help you ............

http://www.codeproject.com/Questions/166232/C-delete-a-registry-key-cannot-get-it-done




回答6:


Please pay attention to 32 or 64 registry. NOTE: RegistryView.Registry64 it's the key of the problem.

This is my implementation that works for me:

public static void DeleteRegistryFolder(RegistryHive registryHive, string fullPathKeyToDelete)
{
    using (var baseKey = RegistryKey.OpenBaseKey(registryHive, RegistryView.Registry64))
    {
        baseKey.DeleteSubKeyTree(fullPathKeyToDelete);
    }
}

Usage:

var baseKeyString = $@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\MyApp";

DeleteRegistryFolder(RegistryHive.LocalMachine, baseKeyString);


来源:https://stackoverflow.com/questions/32250244/delete-a-registry-key-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!