Delete Subkey error (C#) - c#

I have created the following registry key (copied through regedit):
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\test
I would like to now delete this registry key, and so... I have been using the following code and am running into a small error.
RegistryKey regKey;
string regPath_Key = #"Software\Microsoft\Windows\CurrentVersion\test";
regKey = Registry.CurrentUser.OpenSubKey(regPath_Key, true);
if(regKey != null) // Always returns null, even though the key does exist.
{
Registry.CurrentUser.DeleteSubKey(regPath_Key, true);
}
The issue I am having is that the line if(regKey != null) always returns null! I have gone back and checked that the key does in fact exist multiple times - but still the same result. I am going to assume my code has issues somewhere?

Could it be that you are on a 64 bit machine and your project is set to x86 architecture? in that case, verify that the key you state exists under HKCU\Software\Wow6432Node... as every path is redirected to this 32 bit process registry...

You should not include HKEY_CURRENT_USER in the string you pass to Registry.CurrentUser.OpenSubKey(). Instead use
string regPath_Key = #"Software\Microsoft\Windows\CurrentVersion\test";

Related

How Is This Returning NULL

My C# application license manager is returning NULL when checking for a Key's existence even though the key exists and my application is installed. I have tried running as an Administrator and add or removing backslashes in the Key path.
RegistryKey LitenUpKey = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\LitenUp\NIT", false);
if (LitenUpKey == null) {
// Registry Key NOT Found
return false;
}
NOTE: I am building as x64!
As #RbMm pointed out, the issues was in registry reflection between 32 bit and 64 bit. The following question showed me how to choose which view I saw. Here it is.

Create registry key programmatically

I'm working on a C# project and I'm very confused about the creation of a registry key.
I have a Wix Installer. And the and of the setup File, i'm calling a custom action to create sub key (I'v try with the wix feature but it didn't work).
My custom action is :
RegistryKey Nkey = Registry.LocalMachine;
RegistryKey valkey = Nkey.OpenSubKey(Manager.REGKEY, true); //=> REGKEY = "Software\\MyService"
if (valkey == null)
{
valkey = Nkey.CreateSubKey(GestionCertificats.REGKEY);
}
valkey.Close();
Registry.LocalMachine.CreateSubKey(#"SYSTEM\CurrentControlSet\services\eventlog\Application\MyService");
After install, I can see the second key but no the first one. I'm not sure to right understand the operation of the keys. I'm working on a windows 7 64 bit, I'm compiling with "Any CPU" but my application seems to be in x86.
I've try to debug the action. value valkey is not null, but I didn't see the key with regedit. By forcing the CreateSubKey I still have no key.
I don't know what to do, I need help.
just a guess, because everything looks okay.
try closing the key returned. The description of the dispose of a key it clears resources but doesn't say it flushes it.
RegistryKey key = Registry.LocalMachine.CreateSubKey(#"SYSTEM\CurrentControlSet\services\eventlog\Application\MyService");
key.Close();

Delete all values within a registry key using C#

I'm having trouble finding a method to delete all values within a registry key without actually deleting the key itself. I would rather not have to delete the key because I'd rather not deal with re-adding the appropriate permissions back to the key.
I'm trying to have a small method run to simply clear out the values from:
HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Outlook\Resiliency\DisabledItems
We have an add-in that Outlook periodically sticks in here, and our techs have to dig into this key and remove the values, restart Outlook and all is well.
I guess we could just identify the exact name of the value, but that wouldn't be very fun :) and because the way that the values are named in this key, it is not apparent which one points to the correct add-in.
I agree with Ron Beyer. I think this might be what you are looking for? Edit the registry paths and HKLM/HKCU as needed.
string keyPath64Bit = "SOFTWARE\\Wow6432Node\\Krondorian";
RegistryKey localMachine = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey key64Bit = localMachine.OpenSubKey(keyPath64Bit, true);
if (key64Bit != null)
{
var namesArray = key64Bit.GetValueNames();
foreach (string valueName in namesArray)
{
key64Bit.DeleteValue(valueName);
}
}

How to write to HKLM without WOW redirection in WOW scenario?

I have a WOW scenario and want to change the value of Key at
HKLM\Software\Microsoft\ABCD\
I am using this code :
String key = #"SOFTWARE\Microsoft\ABCD\";
RegistryKey reg64key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey reg_64bit_Name = reg64key.OpenSubKey(key);
reg_64bit_Name.SetValue("Name","ahsan");
However this is not working. Can anyone kindly suggest what I need to do here ?
NB:
1. Not working means I am getting the following exception while running the app :
System.NullReferenceException: Object reference not set to an instance of an object.
Firstly, by "this is not working" can you please describe any error messages, exceptions compiler errors?
That being said your code has
reg64key.SetValue("Key","ahsan");
Where you will see you are using the "Key" (as a string). Trying changing this to.
reg64key.SetValue(key,"ahsan");
So you are using your variable instead of the string "Key"
EDIT: After OP changed
After your edits I went back and tried this for myself. Please see the code below (this is tested)
RegistryKey reg64key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
String key = #"SOFTWARE\Microsoft\ABCD";
if (reg64key == null)
throw new Exception("reg64key");
var basekey = reg64key.OpenSubKey(key);
if (basekey == null)
basekey = reg64key.CreateSubKey(key);
basekey.SetValue("Name", "ahsan");
You will see from the code the first thing we do is grab the reg64key for HKLM. Next we check that the reg64key is not null (shouldnt be null but you never know). Next we use the reg64key to open the key "SOFTWARE\Microsoft\ABCD". If this is not found (baseKey == null) then we create the key.
Finally you can set the key as you wish.
Hope this help.s

How to retrieve the value from registry?

I want to retrieve the value from registry. For example like: HKEY_LOCAL_MACHINE\SOFTWARE\Manufacturer's name\Application name\InstallInfo
Under the 'InstallInfo' there are so many variables, like
ProductVersion, WebsiteDescription, WebSiteDirectory, CustomerName, WebSitePort etc.
I want to retrieve some values of these variables. I tried the following code but it returns
'Object reference not set to an instance of an object'
var regKey = Registry.LocalMachine;
regKey = regKey.OpenSubKey(#"SOFTWARE\ABC Limited\ABC Application\InstallInfo");
if (regKey == null)
{
Console.WriteLine("Registry value not found !");
}
else
{
string dirInfo = (string)regKey.GetValue("WebSiteDirectory");
Console.Write("WebSiteDirectory: " + dirInfo);
}
Console.ReadKey();
OpenSubKey returns null when it fails. That's clearly what's happening here.
It's failing because you are looking under the wrong root key. You are looking under HKCU but the key is under HKLM.
So you need
RegistryKey regKey = Registry.LocalMachine.OpenSubKey(
#"SOFTWARE\Manufacturer's name\Application name\InstallInfo");
You must always check the return value when you call OpenSubKey. If it is null then handle that error case.
if (regKey == null)
// handle error, raise exception etc.
The other thing to watch out for is the registry redirector. If your process is a 32 bit process running on a 64 bit system, then you will see the 32 bit view of the registry. That means that your attempt to view HKLM\Softare is transparently redirected to HKLM\Software\Wow6432Node.
Before you convert regKey.GetValue("WebSiteDirectory") to string, You should check it if it is null or not,
if (regKey.GetValue("WebSiteDirectory")!=null)
//do the rest
It may be because you are looking under wrong root key.
It should be:
Registry.CurrentUser
instead of
Registry.LocalMachine
Here you go:
Registry.LocalMachine.CreateSubKey(#"SOFTWARE\Manufacturer's name\Application name\InstallInfo");

Categories