2012-09-10 113 views
0

我想列出一個域上的遠程註冊表文件夾。我能夠列出鍵但不遞歸使用WMI或OpenRemoteBaseKey。 我想獲得註冊表文件夾和子文件夾中的Keyname,Values,Type,最好使用WMI,因爲我需要客戶端上的其他信息。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"]); 
      } 

感謝。

回答

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))); 
} 
+0

完美的,正是我所期待的,非常感謝您的幫助。 – OwenS

+0

它效果很好。 –