我想用十六进制值ffffffff更改注册表项,这是DWORD类型。我有一个功能:
public static void EditRegistryKey(string fullKeyName, string subKeyName,
string keyName, string keyValue, RegistryValueKind keyKind)
{
if (Registry.GetValue(fullKeyName, keyName, null) != null)
{
try
{
RegistryKey myKey = Registry.LocalMachine.OpenSubKey(subKeyName, true);
if (myKey != null)
{
myKey.SetValue(keyName, keyValue, keyKind);
myKey.Close();
}
}
catch (Exception ex)
{
Trace.TraceInformation(ex.Message);
Console.WriteLine(ex.Message);
}
}
}
当我尝试时:
EditRegistryKey( @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config",
@"SYSTEM\CurrentControlSet\Services\W32Time\Config",
"MaxPosPhaseCorrection",
"ffffffff",
RegistryValueKind.DWord);
我收到一个错误,认为converson没有成功。
答案 0 :(得分:0)
因此,基于@Ben Voight的评论,我这样做:将keyValue
参数的类型更改为object
,并将ffffffff
转换为Int32
。我的函数现在使用数字或字符串。
public static void EditRegistryKey(string fullKeyName, string subKeyName,
string keyName, object keyValue, RegistryValueKind keyKind)
{
if (Registry.GetValue(fullKeyName, keyName, null) != null)
{
try
{
RegistryKey myKey = Registry.LocalMachine.OpenSubKey(subKeyName, true);
if (myKey != null)
{
myKey.SetValue(keyName, keyValue, keyKind);
myKey.Close();
}
}
catch (Exception ex)
{
Trace.TraceInformation(ex.Message);
Console.WriteLine(ex.Message);
}
}
}
EditRegistryKey(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config",
@"SYSTEM\CurrentControlSet\Services\W32Time\Config",
"MaxPosPhaseCorrection",
Convert.ToInt32("ffffffff", 16),
RegistryValueKind.DWord);
//Set syncronization to NTP server
EditRegistryKey(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Parameters",
@"SYSTEM\CurrentControlSet\Services\W32Time\Parameters",
"Type",
"NTP",
RegistryValueKind.String);