0

我正在尝试列出域上的远程注册表文件夹。我能够列出密钥,但不能递归地使用 WMI 或 OpenRemoteBaseKey。我想获取 x32 和 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"]);
            }

提前致谢。

4

1 回答 1

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)));
}
于 2012-09-10T20:26:31.637 回答