谷歌今天沒有幫助我,所以我需要你的幫助。有沒有從註冊表中讀取值的可能方法?例如這個位置 - > HKEY_LOCAL_MACHINE> SOFTWARE>的myKey列是測試關鍵從註冊表中讀取價值
回答
64位+ 32位
using Microsoft.Win32;
public static string GetRegistry()
{
string registryValue = string.Empty;
RegistryKey localKey = null;
if (Environment.Is64BitOperatingSystem)
{
localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
}
else
{
localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
}
try
{
localKey = localKey.OpenSubKey(@"Software\\MyKey");
registryValue = localKey.GetValue("TestKey").ToString();
}
catch (NullReferenceException nre)
{
}
return registryValue;
}
不錯,你給了這樣一個完整的答案 - 我個人重構了一點,因爲受你的'if'影響的唯一代碼是'RegistryView.Registry32' /'64'的值。 –
我同意Puzey先生。此外,如果註冊表中不存在「MyKey」,或者由於某些其他原因導致讀取失敗,那麼localKey將爲空,您將得到一個異常。 try/catch語句應該在這裏使用。 – Fredrik
RTRY這樣的:
static object GetRegistryValue(string fullPath, object defaultValue)
{
string keyName = Path.GetDirectoryName(fullPath);
string valueName = Path.GetFileName(fullPath);
return Registry.GetValue(keyName, valueName, defaultValue);
}
,或者您可以使用Registry.LocalMachine
經過代碼項目這個http://www.csharphelp.com/2007/01/registry-ins-and-outs-using-c/
例如:
Read, write and delete from registry with C#
All (you wanted to know) about the Registry with C#, Part 1 of 2
using Microsoft.Win32;
...
RegistryKey masterKey = Registry.LocalMachine.CreateSubKey(
"SOFTWARE\\Test\\Preferences");
if (masterKey == null)
{
Console.WriteLine ("Null Masterkey!");
}
else
{
Console.WriteLine ("MyKey = {0}", masterKey.GetValue ("MyKey"));
}
masterKey.Close();
- 1. 從註冊表中讀取REG_RESOURCE_LIST值
- 2. 從註冊表中讀取值C++
- 3. 從Windows註冊表中讀取值?
- 4. 如何從註冊表中讀取值?
- 5. NSIS:從註冊表中讀取DWORD?
- 6. ERROR_MORE_DATA ----從註冊表中讀取
- 7. 從Perl註冊表中讀取Perl
- 8. 用cmake讀取註冊表值
- 9. C#,壞字符讀取註冊表值
- 10. VBscript - 讀取多個註冊表值
- 11. 如何在VBScript中從註冊表中讀取(默認)值?
- 12. 讀字符串值從註冊表項
- 13. 從VBA讀取註冊表子項
- 14. 通過批處理文件從註冊表中讀取值
- 15. 如何從註冊表中讀取值的名稱和數據?
- 16. 使用WiX的RegistrySearch從註冊表中讀取DWORD值
- 17. Windows從註冊表中讀取顏色值錯誤
- 18. 如何從註冊表中讀取所有值
- 19. 我如何從註冊表讀取REG_BINARY值相關值?
- 20. VB.NET讀取註冊表
- 21. C++讀取註冊表
- 22. 註冊表讀取權限
- 23. 讀取註冊表項
- 24. 讀取註冊表項
- 25. 無法讀取某些值從註冊表
- 26. 從註冊表獲取值問題
- 27. 從註冊表C++崩潰獲取值
- 28. 如何在java中讀取註冊表
- 29. 讀取C#中的註冊表項#
- 30. 如何從註冊表中使用C讀取存儲在註冊表項中的值使用C
你有看的Microsoft.Win32.RegistryKey的文檔? –
我決定幫助你使用Google-Fu - 它可以使用一些改進。 https://www.google.com/search?q=read+registry+values+csharp – BTownTKD
我該如何投票? –