英文:
c# regedit HKEY_LOCAL_MACHINE\SOFTWARE is not read
问题
I need to query the registry through keywords and return all matching registry addresses, but I cannot access the following branches with this function。
HKEY_LOCAL_MACHINE\SOFTWARE\abc\test
HKEY_LOCAL_MACHINE\SYSTEM\test
WHY?
I don't know where to write the question。
Thanks.
keyword="test"
I am in VS, have used administrator rights.
【erro msg】: The requested registry access is not allowed
英文:
I need to query the registry through keywords and return all matching registry addresses, but I cannot access the following branches with this function。
HKEY_LOCAL_MACHINE\SOFTWARE\abc\test
HKEY_LOCAL_MACHINE\SYSTEM\test
WHY?
I don't know where to write the question。
Thanks.
keywork="test"
I am in VS, have used administrator rights.
public static DataTable SearchRegistry(string keyword)
{
// List<string> matchedAddresses = new List<string>();
DataTable dataTable = new DataTable("MyTable");
dataTable.Columns.Add("Name", typeof(string));
dataTable.Columns.Add("HOST", typeof(string));
try
{
SearchRegistryKeys(Registry.ClassesRoot, keyword, "", dataTable);
SearchRegistryKeys(Registry.CurrentUser, keyword, "", dataTable);
SearchRegistryKeys(Registry.LocalMachine, keyword, "", dataTable);
SearchRegistryKeys(Registry.Users, keyword, "", dataTable);
SearchRegistryKeys(Registry.CurrentConfig, keyword, "", dataTable);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return dataTable;
}
public static void SearchRegistryKeys(RegistryKey key, string keyword, string currentPath, DataTable dataTable)
{
string registername = key.Name;
foreach (string subKeyName in key.GetSubKeyNames())
{
using (RegistryKey subKey = key.OpenSubKey(subKeyName))
{
if (subKey != null)
{
if (subKeyName.ToLower().Contains(keyword) )
{
// dataTable.Rows.Add(subKeyName);
dataTable.Rows.Add($"{currentPath}\\{subKeyName}", "");
}
SearchRegistryKeys(subKey, keyword, $"{currentPath}\\{subKeyName}", dataTable);
}
}
}
}
【erro msg】:The requested registry access is not allowed
答案1
得分: 0
以下是翻译好的代码部分:
有两个问题存在于你的代码中。一个是即使具有管理员权限,某些注册表也无法访问。另一个是 GetSubKeyNames 可能会返回 null。以下代码应该可以工作。请确保你的项目是 x64 或者 "Any CPU"(未选中 "Prefer 32-bit")架构。如果项目是 x86,将会搜索 "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node" 而不是 "HKEY_LOCAL_MACHINE\SOFTWARE"。
public static void SearchRegistryKeys(RegistryKey key, string keyword, string currentPath, DataTable dataTable)
{
try
{
string registername = key.Name;
// GetSubKeyNames 可能会返回 null
var subKeyNames = key.GetSubKeyNames();
if (subKeyNames == null)
{
return;
}
foreach (string subKeyName in subKeyNames)
{
try
{
using (RegistryKey subKey = key.OpenSubKey(subKeyName))
{
if (subKey != null)
{
if (subKeyName.ToLower().Contains(keyword))
{
dataTable.Rows.Add($"{currentPath}\\{subKeyName}", "");
}
SearchRegistryKeys(subKey, keyword, $"{currentPath}\\{subKeyName}", dataTable);
}
}
}
catch (SecurityException)
{
// 某些注册表无法访问
Console.WriteLine($"无法访问 {currentPath}\\{subKeyName}");
}
catch
{
throw;
}
}
}
catch (SecurityException)
{
// 某些注册表无法访问
Console.WriteLine($"无法访问 {currentPath}");
}
catch
{
throw;
}
}
希望这对你有所帮助!如果你有任何其他问题或需要进一步的协助,请随时告诉我。
英文:
There were two issues in you code. One is that some registry is unaccessible even if with admin right. The other is that GetSubKeyNames might return null. The following code should work. Please make sure that your project is x64 or "Any CPU" (With "Prefer 32-bit" unchecked) architecture. If the project is x86, "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node" will be searched instead of "HKEY_LOCAL_MACHINE\SOFTWARE".
public static void SearchRegistryKeys(RegistryKey key, string keyword, string currentPath, DataTable dataTable)
{
try
{
string registername = key.Name;
//GetSubKeyNames might return null
var subKeyNames = key.GetSubKeyNames();
if (subKeyNames == null)
{
return;
}
foreach (string subKeyName in subKeyNames)
{
try
{
using (RegistryKey subKey = key.OpenSubKey(subKeyName))
{
if (subKey != null)
{
if (subKeyName.ToLower().Contains(keyword))
{
dataTable.Rows.Add($"{currentPath}\\{subKeyName}", "");
}
SearchRegistryKeys(subKey, keyword, $"{currentPath}\\{subKeyName}", dataTable);
}
}
}
catch (SecurityException)
{
//Some registry is unaccessible
Console.WriteLine($"Cannot access {currentPath}\\{subKeyName}");
}
catch
{
throw;
}
}
}
catch (SecurityException)
{
//Some registry is unaccessible
Console.WriteLine($"Cannot access {currentPath}");
}
catch
{
throw;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论