Today I have a implement a method to create/delete a specify registry key, it alwasy throw exception say the registry key does not exist, it confused me that it was just write by myself...
Now, I get it: the registry key is like a node of the tree, path; the value is a value
private static void DeleteRegistryKeyValue(string serverName, string keyPath, string keyName)
{
using (RegistryKey regKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, serverName))
{
if (null == regKey)
{
throw new Exception("Invalid Server Name specified " + serverName);
}
try
{
RegistryKey parameterSystem = regKey.OpenSubKey(keyPath,true);
if (null != parameterSystem)
{
parameterSystem.DeleteSubKey(keyName);
}
}
catch (Exception ex)
{
Console.WriteLine("Register key deleted failed, please delete it manually if needed");
Console.WriteLine(ex.ToString());
}
}
}
private static void CreateRegistryKeyValue(string serverName, object value, RegistryValueKind valueKind, string keyPath, string keyName)
{
using (RegistryKey regKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, serverName))
{
if (null == regKey)
{
throw new Exception("Invalid Server Name specified " + serverName);
}
using (RegistryKey subKey = regKey.CreateSubKey(keyPath))
{
subKey.SetValue(keyName, value, valueKind);
}
}
}