我正在尝试列出域上的远程注册表文件夹。我能够列出键但不能递归使用WMI或OpenRemoteBaseKey。 我想获得关于x32&的注册表文件夹和子文件夹的Keyname,Values,Type x64,最好使用WMI,因为我需要客户端的其他信息。
这是我的代码,但需要改进:
foreach (string strComputer in arrComputers)
{
Console.WriteLine("==========================================");
Console.WriteLine(" Computer: " + strComputer);
Console.WriteLine("==========================================");
ManagementClass classInstance =
new ManagementClass("\\\\" + strComputer + "\\root\\DEFAULT",
"StdRegProv", null);
ManagementBaseObject inParams =
classInstance.GetMethodParameters("EnumValues");
inParams["sSubKeyName"] = "SOFTWARE\\IPS";
ManagementBaseObject outParams =
classInstance.InvokeMethod("EnumValues", inParams, null);
Console.WriteLine("Out parameters:");
Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
Console.WriteLine("sNames: " + outParams["sNames"]);
Console.WriteLine("Types: " + outParams["Types"]);
}
提前致谢。
答案 0 :(得分:2)
我的代码中没有看到任何递归。 这是一个例子。在递归所有子子键之前,ParseKey将使用'ParseChildValues'转储值。
using Microsoft.Win32;
...
/// <summary>
/// This method returns a list of registry values from a given key in .reg format.
/// </summary>
/// <param name="key">Registry key from which to obtain values.</param>
/// <returns>A CR-separated list of all the registry values for a given key.</returns>
private static string ParseChildValues(RegistryKey key)
{
if (key.IsNull())
return string.Empty;
StringBuilder sb = new StringBuilder();
foreach(string val in key.GetValueNames())
{
sb.AppendFormat("{0}=\"{1}\"", string.IsNullOrEmpty(val) ? "@" : val, key.GetValue(val));
sb.AppendLine();
}
return sb.ToString();
}
/// <summary>
/// Parses the SubKeys of a given RegistryKey.
/// </summary>
/// <param name="key">A registry key</param>
/// <returns>Lots of important data!</returns>
private static string ParseKey(RegistryKey key)
{
if (key.IsNull())
return string.Empty;
StringBuilder sb = new StringBuilder();
sb.AppendLine();
sb.AppendFormat("[{0}]", key.Name);
sb.AppendLine();
sb.Append(ParseChildValues(key));
foreach (var subKey in key.GetSubKeyNames())
{
sb.Append(ParseKey(key.OpenSubKey(subKey)));
}
return sb.ToString();
}
static void Main(string[] args)
{
// Dump the contents of HKEY_CLASSES_ROOT\*
Console.WriteLine("Getting the contents of HKCR\\* on the local machine");
RegistryKey localHKCR = Registry.ClassesRoot.OpenSubKey("*", RegistryKeyPermissionCheck.ReadSubTree);
Console.WriteLine(ParseKey(localHKCR));
// Dump the contents of HKEY_CLASSES_ROOT\* on a remote machine.
Console.WriteLine();
Console.WriteLine("Getting the contents of HKCR\\* via OpenRemoteBaseKey");
RegistryKey remoteHKCR = RegistryKey.OpenRemoteBaseKey(RegistryHive.ClassesRoot, "MACHINENAME");
Console.WriteLine(ParseKey(remoteHKCR.OpenSubKey("*", RegistryKeyPermissionCheck.ReadSubTree)));
}