2017-02-13 76 views
-2

我的程序在注册表中包含相当多的值,并且获取这些值的名称根本没有问题;真正的问题是从这些特定的值获取数据。从注册表中获取数据[C#]

这是我的一段代码。假设“paths.mainKey”是“HKEY_LOCAL_MACHINE \ SOFTWARE \ WOW6432Node”。还假设“paths.subKey”是“HKEY_LOCAL_MACHINE \ SOFTWARE \ WOW6432Node \ Razer Chroma SDK”(显然不是我的密钥,只是用它作为例子)。

private void ReadRegistry() 
{ 
    string[] allSubKeys = Registry.LocalMachine.OpenSubKey(paths.mainKey).GetSubKeyNames(); 
    if (allSubKeys.Contains("Razer Chroma SDK")) 
    { 
     string[] allDayPolicies = Registry.LocalMachine.OpenSubKey(paths.subKey).GetValueNames(); 
     foreach (string value in allDayPolicies) 
     { 
      //Read data from the values? 
     } 
    } 
} 

This is a visual representation of what I'm trying to get from the Registry.

任何人都知道如何获得这些数据?

+0

如何让这些价值观的问题吗?你使用过'GetValue()'方法吗? –

+0

您删除了关于您的电脑与笔记本电脑关机的问题。我正要向你建议一种不同的方式来确定时间 - 试试这个:'DateTime.ParseExact(TIME_START_TIME,“H tt”,CultureInfo.InvariantCulture).TimeOfDay' – Enigmativity

+0

@Enigmativity我很欣赏这个建议,但是,我想通了解决我的问题;当我将时间从注册表转换为军事时间时,我没有在时间是“上午12点”的时候添加一个案例。我的代码的工作方式,这意味着它变成了12 PM,使重新启动代码块不执行,因为条件不满足。 – MTS11648

回答

0

您可以使用GetValue()

private void ReadRegistry() 
{ 
    string[] allSubKeys = Registry.LocalMachine.OpenSubKey(paths.mainKey).GetSubKeyNames(); 
    if (allSubKeys.Contains("Razer Chroma SDK")) 
    { 
     var subKey = Registry.LocalMachine.OpenSubKey(paths.subKey); 
     string[] allDayPolicies = subKey.GetValueNames(); 
     foreach (string name in allDayPolicies) 
     { 
      var value = subKey.GetValue(name); 
      // do something with value 
     } 
    } 
} 
+0

不知道这是那么容易。谢谢。 – MTS11648