獲取註冊表
回答
使用Microsoft.Win32.Registry對象:
private Dictionary<string, object> GetRegistrySubKeys()
{
Dictionary<string, object> valuesBynames = new Dictionary<string, object>();
const string REGISTRY_ROOT = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\";
//Here I'm looking under LocalMachine. You can replace it with Registry.CurrentUser for current user...
using (RegistryKey rootKey = Registry.CurrentUser.OpenSubKey(REGISTRY_ROOT))
{
if (rootKey != null)
{
string[] valueNames = rootKey.GetValueNames();
foreach (string currSubKey in valueNames)
{
object value = rootKey.GetValue(currSubKey);
valuesBynames.Add(currSubKey, value);
}
}
rootKey.Close();
}
return valuesBynames;
}
確保添加相應的 「使用」 的聲明:
using Microsoft.Win32;
隨着詞典<字符串,對象>我放什麼?或者我如何使用你的代碼? –
你只是'var registrykeys = GetRegistrySubKeys();'然後使用該變量你會[任何其他字典
謝謝,我的代碼工作! –
如果鍵的值的字符串
string[] names = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\").GetSubKeyNames();
//dont call both at same time....
string[] values = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\").GetValueNames();
foreach (string key in values)
{
//do your stuff...
}
需要導入命名空間
using Microsoft.Win32;
//also...
//retrieves the count of subkeys in the key.
int count = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion").SubKeyCount;
還要檢查這篇文章,可以幫助你在你的任務 http://www.codeproject.com/Articles/3389/Read-write-and-delete-from-registry-with-C
編輯:
取自codeproject文章閱讀的關鍵
public string Read(string KeyName)
{
// Opening the registry key
RegistryKey rk = baseRegistryKey ;
// Open a subKey as read-only
RegistryKey sk1 = rk.OpenSubKey(subKey);
// If the RegistrySubKey doesn't exist -> (null)
if (sk1 == null)
{
return null;
}
else
{
try
{
// If the RegistryKey exists I get its value
// or null is returned.
return (string)sk1.GetValue(KeyName.ToUpper());
}
catch (Exception e)
{
// AAAAAAAAAAARGH, an error!
ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
return null;
}
}
}
調用'OpenSubKey'兩次(或三次)是一個壞主意 - 如果註冊表在兩次調用之間發生變化(罕見但可能),您的代碼將崩潰(或更糟!),因爲兩個數組大小不同,完全不同的數據。 – mcmonkey4eva
我已經將代碼編輯爲string [] names = Registry.CurrentUser.OpenSubKey(@「SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run」)。GetSubKeyNames(); string [] values = Registry.CurrentUser.OpenSubKey(@「SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run」)。GetValueNames(); foreach(字符串鍵值) MessageBox.Show(names +「,Value:」+ values);並且它的返回值是:System.String [],值:System.String [] –
... @DanielJones - 呃...你有任何C#經驗嗎?你似乎只是...做隨機的東西......並不知道什麼數組... ... – mcmonkey4eva
- 1. 獲取註冊表值C#
- 2. 獲取註冊表值
- 3. 使用C獲取註冊表值#
- 4. 獲取遠程註冊表值
- 5. 使用Python獲取註冊表信息
- 6. 從註冊表獲取值問題
- 7. 駱駝 - 獲取jndi註冊表
- 8. 從註冊表獲取子目錄
- 9. 從註冊表C++崩潰獲取值
- 10. 註冊表項獲取值返回NULL
- 11. 獲取註冊表項刪除權限
- 12. 芹菜獲取註冊任務列表
- 13. 使用PHP註冊表格獲取
- 14. 獲取註冊表項的值
- 15. 從註冊表中獲取數據[C#]
- 16. 獲取已註冊的註釋
- 17. 從註冊表獲得
- 18. 從註冊表獲得C#
- 19. VB.NET讀取註冊表
- 20. C++讀取註冊表
- 21. 註冊表讀取權限
- 22. 讀取註冊表項
- 23. 讀取註冊表項
- 24. 雖然註冊獲取註冊日期時間和散列它
- 25. 取消註冊DelegateCommand /獲取使用的委託命令列表
- 26. 獲取Windows服務來讀取註冊表HKLM
- 27. 推送註冊表取消註冊連接
- 28. 註冊並取消註冊angular js手錶
- 29. pushwoosh註冊並取消註冊
- 30. DLL註冊和取消註冊
避免調用都在同一時間丹尼爾,這僅僅是爲了演示(我不確切知道你需要什麼)。 – terrybozzio