从注册表中的值获取数据 [C#]
Getting Data from Values in the Registry [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.
有人知道如何获取这些数据吗?
您可以使用 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
}
}
}
我的程序在注册表中包含相当多的值,获取这些值的名称完全没有问题;真正的问题是从这些特定值中获取数据。
这是我的一段代码。假设 "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.
有人知道如何获取这些数据吗?
您可以使用 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
}
}
}