Registry key and Registry value

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);
                }
            }
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值