2013-06-30 107 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