How to delete a registry value in C#
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.
Delete a Registry key using C#
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!
How to delete a Registry key?
Here's one approach. Note that you have to pass true to OpenSubKey in order to get Write permission.
var hklm = Microsoft.Win32.Registry.LocalMachine;
var subkey = hklm.OpenSubKey("Software\\Wow6432Node\\WindowsApplication1", true);
subkey.DeleteSubKey("Status");
How to delete a registry value in C#?
this should work
string keyName = @"Software\Microsoft\Cryptography";
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(keyName, true))
{
if (key == null)
{
MessageBox.Show("Value Cannot Be Found");
}
else
{
key.DeleteValue("MachineGuid");
}
}
I dont recommend deleting it though lol
How to delete value in registry?
If you want to delete the run history, you want to delete every value of the registry key @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU"
minus the (default) one.
(Luckily, the framework ignore the default one when you enumerate it)
On your code, instead, you are passing the entire key path to the key.DeleteValue()
which is wrong.
A right approach is to first enumerate all the values of the key and then delete, here is an example:
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU", true))
{
string[] values = key.GetValueNames();
foreach (var item in values)
{
key.DeleteValue(item);
}
}
Reference:
https://support.microsoft.com/en-us/kb/142298
Delete all values within a registry key using C#
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 delete registry key by using RegistryKey object as handle
Undocumented but calling RegDeleteKey(hKey, @"")
(advapi32) will delete a key by handle (works all the way back to Win95/NT4).
I don't know what the behavior on symlinks is. It most likely deletes the link (if you specifically opened the symlink) and not the target but you just have to check to be sure.
If you don't want to rely on undocumented behavior you have to call ZwDeleteKey
(ntdll) as suggested by RbMm.
C# check and delete registry key problems
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!
Related Topics
Put Wpf Control into a Windows Forms Form
Serialize Property, But Do Not Deserialize Property in JSON.Net
How to Select Xml Nodes with Xml Namespaces from an Xmldocument
Force JSON.Net to Include Milliseconds When Serializing Datetime (Even If Ms Component Is Zero)
The Calling Thread Must Be Sta, Because Many UI Components Require This in Wpf
Shortcut for "Null If Object Is Null, or Object.Member If Object Is Not Null"
Does Distinct() Method Keep Original Ordering of Sequence Intact
How to Make a Property Protected and Internal in C#
Additional Text Encountered After Finished Reading JSON Content:
Transitive References in .Net Core 1.1
Conditional Compilation Depending on the Framework Version in C#
How to Interrupt Console.Readline
Is There Some Way to Handle Async/Await Behind an Asmx Service
Meanings of Declaring, Instantiating, Initializing and Assigning an Object