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);
}
}
Related
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.
To begin, I recognize that this question appears similar to others. However, my research thus far has not found a solution to the specific problem I am facing, just a lot of solutions to problems with similar circumstance.
I am new to registry functions, so I've been using a VM to mess around with them and see what I can do. Currently, I am trying to test the creation, reading, and subsequent deletion of a subKey and HKEY_CURRENT_USER. At the moment, I can do everything in that list except deletion. The relevant code is as follows:
//This first sample will attempt to create a test key under HKEY_CURRENT_USER
Console.WriteLine("Creating subkey under HKEY_CURRENT_USER");
RegistryKey testKey2 = Registry.CurrentUser.CreateSubKey("SubKeyTester");
Console.WriteLine("testKey2 is now assigned to {0}", testKey2);
//This ensures that testKey2 is the value that I think it is
Console.WriteLine("testKey2 value = {0}\n", testKey2);
with an output of:
Beginning test...
Creating subkey under HKEY_CURRENT_USER
testKey2 is now assigned to HKEY_CURRENT_USER\SubKeyTester
testKey2 value = HKEY_CURRENT_USER\SubKeyTester
Notably, testKey2 has stored "HKEY_CURRENT_USER\SubKeyTester" rather than the "SubKeyTester" that I expected.
After this, I'm able to check the subkeys under HKEY_CURRENT_USER and verify that yes, "SubKeyTester" is indeed present among the CurrentUser subkeys. Now, I just need to delete it. My code is as follows:
//This portion of the test will attempt to delete SubKeyTester from
//HKEY_CURRENT_USER
Console.WriteLine("Attempting to delete test subkey\n");
try
{
Registry.CurrentUser.DeleteSubKeyTree(testKey2.ToString());
Console.WriteLine("Target has been deleted\n");
}
catch(Exception e)
{
Console.WriteLine("The key targeted for deletion... is not found.\nError: {0}\n", e);
}
//Another safety check to verify that only SubKeyTester has been deleted
Console.WriteLine("There are {0} subkeys under {1}.",
Registry.CurrentUser.SubKeyCount.ToString(), Registry.CurrentUser.Name);
foreach (string subKeyName in Registry.CurrentUser.GetSubKeyNames())
Console.WriteLine(subKeyName);
testKey2.Close();
The output informs me:
"Error: System.ArgumentException: Cannot delete a subkey tree because the subkey does not exist."
It then lists all the subkeys under HKEY_CURRENT_USER, which still includes the testKey "SubKeyTester".
I believe the problem could be solved by just hard-coding the path to that subkey in the DeleteSubKeyTree call, but I want to avoid that. I'd rather just be able to invoke testKey2 as a parameter and delete the key that way. Is there a way to do that?
I have found my error, and I was on the right track. The correct code is as follows:
Console.WriteLine("Creating subkey under HKEY__CURRENT_USER\n");
const string testKey2 = "SubKeyTester";
Registry.CurrentUser.CreateSubKey(testKey2);
This way, testKey2 is always "SubKeyTester". Then, the only other alteration needed is to the delete function's parameter.
Registry.CurrentUser.DeleteSubKeyTree(testKey2);
removing the ToString() method.
I believe the problem was as I said, that testKey2 was getting "HKEY_CURRENT_USER//SubKeyTester" instead of just "SubKeyTester". But this way, testKey2 only gets "SubKeyTester", which allows for a correct filepath to the appropriate key.
I had tried using
Registry.DeleteSubKeyTree(testKey2.ToString());
to get around the pathing error, but "Registry" does not have a DeleteSubKeyTree method, so that simply didn't work.
Also, this solution does not require a .close() statement, because no key was ever opened.
Not a solution, but I found the error message is a red herring.
One of the subkeys had permissions preventing it from being deleted.
Confirmed in Registry Editor too:
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();
i was wondering for a project of mine, is it possible to change the MachineGuid from the registry or any other way? I've seen it in multiple applications and I can't do it myself..This is my code
RegistryKey reg = Registry.LocalMachine;
reg.OpenSubKey("SOFTWARE\\Microsoft\\Cryptography");
reg.DeleteValue("MachineGuid");
reg.Close();
consolebox.AppendText("MachineGuid should be changed!\n");
But it doesn't work.. it doesn't delete the MachineGuid value, which would automatically regenerate in about a second....
The error says that it doesnt find the value.. that MachineGuid doesn't exist... but when i go to regedit it does?
If i don't run the application as an Administrator, it says the value got deleted, but if i do it says it doesn't exist....
You have a couple of issues, first you don't open the key to be writeable and you don't use the result of OpenSubKey. That method returns the key you actually opened.
RegistryKey reg = Registry.LocalMachine;
using(var key = reg.OpenSubKey("SOFTWARE\\Microsoft\\Cryptography", true)) // writeable
{
key.DeleteValue("MachineGuid");
}
The RegistryKey object implements IDisposable, better apply the using pattern in that case to close and dispose the key.
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";