我有一个在特定键路径中搜索注册表键值的代码。在注册表项SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}
中找到所有注册表0000 , 0001 , 0002 , 0003
项末尾带有等的键。{4D36E972-E325-11CE-BFC1-08002BE10318}
. 在每个键(例如0007
)中都调用了子键NetCfgInstanceId
,其中保存了网络接口卡的 ID 值,如下所示{C80949A4-CEDA-4F29-BFE2-059856D7F745}
。如果找到值,方法返回键路径!Cannot convert type 'char' to 'string'
问题是一个错误
foreach (string key_value in key.GetValue("NetCfgInstanceId").ToString())
完整代码是
public string key_path(RegistryKey root, string root_path, string search_key)
{
string path = string.Empty;
foreach (string keyname in root.GetSubKeyNames())
{
try
{
using (RegistryKey key = root.OpenSubKey(keyname, true))
{
foreach (string key_value in key.GetValue("NetCfgInstanceId").ToString())
{
if (key_value == search_key)
{
string reg_path = (string)key.GetValue("NetCfgInstanceId");
path = reg_path;
}
else
{
path = "Can't find key !";
}
}
}
}
catch (System.Security.SecurityException)
{
//Do nothing !!!
}
}
return path;
}
private void kryptonButton4_Click(object sender, EventArgs e)
{
var answer = key_path(Registry.LocalMachine, @"SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}", "{C80949A4-CEDA-4F29-BFE2-059856D7F745}");
MessageBox.Show(answer);
}
如何解决这个问题?