2013-06-30 105 views
0

鍵如何獲得使用foreach語句如何將我做到這一點的註冊表文件夾的所有註冊表子鍵,如獲取註冊表

SOFTWARE\Microsoft\Windows\CurrentVersion\Run\ 

+0

避免調用都在同一時間丹尼爾,這僅僅是爲了演示(我不確切知道你需要什麼)。 – terrybozzio

回答

1

使用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; 
+0

隨着詞典<字符串,對象>我放什麼?或者我如何使用你的代碼? –

+0

你只是'var registrykeys = GetRegistrySubKeys();'然後使用該變量你會[任何其他字典](http://www.dotnetperls.com/dictionary) – mcmonkey4eva

+0

謝謝,我的代碼工作! –

0

如果鍵的值的字符串

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; 
     } 
    } 
} 
+0

調用'OpenSubKey'兩次(或三次)是一個壞主意 - 如果註冊表在兩次調用之間發生變化(罕見但可能),您的代碼將崩潰(或更糟!),因爲兩個數組大小不同,完全不同的數據。 – mcmonkey4eva

+0

我已經將代碼編輯爲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 [] –

+0

... @DanielJones - 呃...你有任何C#經驗嗎?你似乎只是...做隨機的東西......並不知道什麼數組... ... – mcmonkey4eva