[英]Read value of registry in c#
我有C#應用程序,其中包含用於檢索注冊表值並檢查其values.registry值的代碼,該代碼以以下方式存儲:
主鍵:
Name:user123
Isregistered:no
但是,如果Isregistered返回“ no” ,它將顯示相應的消息。
我收到這樣的錯誤
你調用的對象是空的。
C#代碼:
RegistryKey reg = Registry.LocalMachine.OpenSubKey(@"HKEY_CURRENT_USER\\MainKey", true);
string currentKey;
currentKey = reg.GetValue("Isregistered", true).ToString();
if (currentKey == "yes")
{
Console.WriteLine("availble");
}
else
{
Console.WriteLine("Not availble");
}
我在錯誤
“ currentKey = reg.GetValue(” Isregistered“,true).ToString();”
我在您的代碼中看到兩個問題:
1)
// You're searching for HKEY_CURRENT_USER in HKEY_LOCAL_MACHINE
// Use Registry.CurrentUser instead.
RegistryKey reg = Registry.LocalMachine.OpenSubKey(@"HKEY_CURRENT_USER\\MainKey", true);
string currentKey;
currentKey = reg.GetValue("Isregistered", true).ToString();
在這里找到有關CurrentUser的更多信息
2)另一方面是在注冊表路徑中不要同時使用@或\\\\。 即
OpenSubKey(@"HKEY_CURRENT_USER\MainKey", true);
要么
OpenSubKey("HKEY_CURRENT_USER\\MainKey", true);
在此處找到有關逐字字符串文字的更多信息
您已經使用@並轉義了反斜杠。 另外,您還需要確保當前鍵不為null。
請參閱此處的GetValue調用: http : //msdn.microsoft.com/zh-cn/library/microsoft.win32.registry.getvalue.aspx
祝好運!
您正在嘗試使用LocalMachine注冊表打開HKEY_CURRENT_USER。 請改用Registry.CurrentUser.OpenSubKey(@“ MainKey”,true)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.