MartyP
April 22nd, 2006, 12:08 PM
Hello,
Has any tried deleting values from a registry subkey using .NET? I'm having trouble using the DeleteValue() method, because it always throws an exception whenever I try to delete a value, even though I know the value exists and I'm logged in as an Administrator on Windows XP.
This code always gets a System.UnauthorizedAccessException, with a detail that it "Cannot write to the registry key." I've tried deleting values other than ones in "Software\Microsoft\Windows\CurrentVersion\Run\" but that didn't work either.
Also, I know how to do it in C++, and that works, but I can't get this to work in .NET.
Here is the code I've tried using:
//if the value name exists, delete it
string subKeyName = @"Software\Microsoft\Windows\CurrentVersion\Run\"; //subkey name
string valueName = @"App Name"; //name of the more specific key that will hold the value, "" means (Default)
try
{
RegistryKey reg = Registry.LocalMachine.OpenSubKey(subKeyName);
if (reg != null)
{
reg.DeleteValue(valueName);
reg.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString());
}
Also, I've tried this code snippet from http://www.c-sharpcorner.com/Tutorials/WinRegTutCSMCB.asp, but that didn't work for me either. (I created a string value as the code says using regedit.exe.)
// Delete the key value
RegistryKey delKey = Registry.LocalMachine.OpenSubKey("Software\\");
delKey.DeleteValue("MCBInc");
Any thoughts?
Many thanks,
MartyP
Has any tried deleting values from a registry subkey using .NET? I'm having trouble using the DeleteValue() method, because it always throws an exception whenever I try to delete a value, even though I know the value exists and I'm logged in as an Administrator on Windows XP.
This code always gets a System.UnauthorizedAccessException, with a detail that it "Cannot write to the registry key." I've tried deleting values other than ones in "Software\Microsoft\Windows\CurrentVersion\Run\" but that didn't work either.
Also, I know how to do it in C++, and that works, but I can't get this to work in .NET.
Here is the code I've tried using:
//if the value name exists, delete it
string subKeyName = @"Software\Microsoft\Windows\CurrentVersion\Run\"; //subkey name
string valueName = @"App Name"; //name of the more specific key that will hold the value, "" means (Default)
try
{
RegistryKey reg = Registry.LocalMachine.OpenSubKey(subKeyName);
if (reg != null)
{
reg.DeleteValue(valueName);
reg.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString());
}
Also, I've tried this code snippet from http://www.c-sharpcorner.com/Tutorials/WinRegTutCSMCB.asp, but that didn't work for me either. (I created a string value as the code says using regedit.exe.)
// Delete the key value
RegistryKey delKey = Registry.LocalMachine.OpenSubKey("Software\\");
delKey.DeleteValue("MCBInc");
Any thoughts?
Many thanks,
MartyP