Registry class. If key exist - c#

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
}
}

Related

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.

Find and Read registry key within CLR

I need to check the availability and also read some registry key by CLR(C#), registry keys already written by another application.
As a sample:
public bool IsKeyAvailable(string KeyID)
{
string keyToRead = #"Software\myRoot\myApp\" + KeyID;
using (RegistryKey regKey = Registry.CurrentUser.OpenSubKey(keyToRead, RegistryKeyPermissionCheck.ReadSubTree))
{
if (regKey == null)
return false;
return true;
}
}
Checking & reading code are working fine outside of the CLR, but within the CLR the same code doesn't working, Already signed the CLR and assembly created WITH PERMISSION_SET = UNSAFE.
What could be missed for this scenario to find and read my registry keys by CLR?
Use this code :
Registry.LocalMachine.OpenSubKey("SOFTWARE", true);
RegistryKey masterKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\yourapp\yourkey");
string value = "";
if (masterKey != null)
{
value = masterKey.GetValue("yourvalue").ToString();
}
masterKey.Close();

Attempting to delete registry keys with subkeys results in an error

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");
}

Categories