Attempting to delete registry keys with subkeys results in an error - c#

When I try to delete a key in HKCU that has subkeys I get an error.
Here is the code I am using:
using (RegistryKey regkey = Registry.CurrentUser.OpenSubKey(#"Software\Policies\", true))
{
if (regkey.OpenSubKey("Google") != null)
{
regkey.DeleteSubKey("Google");
}
}
The error I get:
Registry key has subkeys and recursive removes are not supported by this method.
How can I overcome it?

Use the RegistryKey.DeleteSubKeyTree method.
RegistryKey.DeleteSubKeyTree Method (String)
Deletes a subkey and any child subkeys recursively.
using(RegistryKey regkey = Registry.CurrentUser.OpenSubKey(#"Software\Policies\", true))
{
if (regkey.OpenSubKey("Google") != null)
{
regkey.DeleteSubKeyTree("Google");
}
}

using(var regkey = Registry.CurrentUser.OpenSubKey(#"Software\Policies\", true))
{
regkey?.DeleteSubKeyTree("Google");
}

Related

search an uninstall from registry key C#

I want to uninstall some app programmatically. I'm searching in this path in win registry:
\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
the code for searching and uninstalling is the following:
public string UninstallCMD;
public bool SearchApp(string p_name)
{
string displayName;
RegistryKey key;
key = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
foreach (String keyName in key.GetSubKeyNames())
{
RegistryKey subkey = key.OpenSubKey(keyName);
displayName = subkey.GetValue("DisplayName") as string;
if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
{
UninstallCMD = subkey.GetValue("UninstallString") as string;
return true;
}
}
}
My problem is that not all the key were read. The key that I want to be read is {56DDDFB8-7F79-4480-89D5-25E1F52AB28F} but is ignored like the other in { } (in this image you can see the key ignored)
The key without { } were read normally (i.e. 7-zip, VLC media player, ...)
I don't know why but after recompiling solution, everything work fine.

Registry class. If key exist

Im using the Microsoft.Win32.Registry class. Im trying to make a if RegKey exist statement but don't know how
I want something like this:
RegistryKey key = Registry.CurrentUser.CreateSubKey(#"SOFTWARE\test");
if(key.keyExist("yourKey")) Console.WriteLine("yourKey exist!");
As far as I know, the SubKey is stored in a path in the system.
So you can do something like this to check out if the SubKey exists:
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(#"SOFTWARE\test"))
{
if (key != null)
{
Console.WriteLine("yourKey exist!");
}
else
{
// e.g. create SubKey
}
}

How to read registry data from c#

In my c# program, I'm running a python process. Currently I'm using a hardcoded path for the python.exe, but I want to use the windows registry to return the path to me.
I have found the python path info in the Windows registry under:
HKEY_CURRENT_USER\Software\Python\PythonCore\3.7-32\InstallPath
with some googling I found the following solution:
https://stackoverflow.com/a/18234755/7183609
but when I run my code the variable key is always null
try
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Python\\PythonCore\\3.7-32\\InstallPath"))
{
if (key != null)
{
// do things
}
}
}
catch (Exception ex)
{
// do other things
}
Am I doing something wrong, do I need to add something to make it work?
John Wu pointed out that LocalMachine was wrong.
changed
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(#"HKEY_CURRENT_USER\Software\Python\PythonCore\3.7-32\InstallPath"))
to
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(#"HKEY_CURRENT_USER\Software\Python\PythonCore\3.7-32\InstallPath"))
Use Registry.CurrentUser instade of Registry.LocalMachine
try
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Python\\PythonCore\\3.7-32\\InstallPath"))
{
if (key != null)
{
// do things
}
}
}
catch (Exception ex)
{
// do other things
}

C# check and delete registry key problems

I'm trying to check (and delete) registry key. Code:
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
if (registryKey != null) // <- !!!!!!!!! problem is here, i think
{
registryKey.SetValue("MyApp", codeBase);
}
else
{
registryKey.DeleteValue("MyApp");
}
After creating value, application doesnt see new value and doesnt delete it.
What's wrong with this code? Thanks.
Your operations for adding and deleting from the registry look good just that there is an issue with your if-else condition. If you want to check and then delete a registry entry, you should do it like this:
string KeyName = #"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
string valueName = "MyApp";
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
if (Registry.GetValue(KeyName, valueName, null) != null)
{
RegistryKey registryKey = Registry.CurrentUser
.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
registryKey.DeleteValue(valueName);
}
Registry.GetValue(KeyName, valueName, null) is a better way to do a null check instead of doing registryKey != null because registryKey only gets the ..CurrentVersion\\Run subkey. Instead you should drill down the actual key for your app (eg. MyApp).
Hope this helps!

Delete Registry Subkey C# [duplicate]

I can get/set registry values using the Microsoft.Win32.Registry class. For example,
Microsoft.Win32.Registry.SetValue(
#"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run",
"MyApp",
Application.ExecutablePath);
But I can't delete any value. How do I delete a registry value?
To delete the value set in your question:
string keyName = #"Software\Microsoft\Windows\CurrentVersion\Run";
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName, true))
{
if (key == null)
{
// Key doesn't exist. Do whatever you want to handle
// this case
}
else
{
key.DeleteValue("MyApp");
}
}
Look at the docs for Registry.CurrentUser, RegistryKey.OpenSubKey and RegistryKey.DeleteValue for more info.
To delete all subkeys/values in the tree (~recursively), here's an extension method that I use:
public static void DeleteSubKeyTree(this RegistryKey key, string subkey,
bool throwOnMissingSubKey)
{
if (!throwOnMissingSubKey && key.OpenSubKey(subkey) == null) { return; }
key.DeleteSubKeyTree(subkey);
}
Usage:
string keyName = #"Software\Microsoft\Windows\CurrentVersion\Run";
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName, true))
{
key.DeleteSubKeyTree("MyApp",false);
}
RegistryKey registrykeyHKLM = Registry.LocalMachine;
string keyPath = #"Software\Microsoft\Windows\CurrentVersion\Run\MyApp";
registrykeyHKLM.DeleteValue(keyPath);
registrykeyHKLM.Close();
RegistryKey.DeleteValue
string explorerKeyPath = #"Software\TestKey";
using (RegistryKey explorerKey = Registry.CurrentUser.OpenSubKey(explorerKeyPath, writable: true))
{
if (explorerKey != null)
{
explorerKey.DeleteSubKeyTree("TestSubKey");
}
}
I Needed something a little different, I just needed to delete everything a key contained. Therefore the below
Registry.LocalMachine.DeleteSubKeyTree(#"SOFTWARE\YourNeededKeyThatHasMany\");
Note that here its using LocalMachine so it's looking in "HKEY_LOCAL_MACHINE" for the Key to delete its SubTreeKeys. Was simpler for me to do this and would've liked to see this simple answer here.

Categories