0

WMIとSystem.Managementライブラリを使用して次のコードをC#に変換しようとしています。誰かが私を正しい方向に向けたり、助けを提供したりできますか?

var IMPORT_CHILDREN = 0; // Recursively imports the subkeys of the specified key. 
var IMPORT_INHERITED = 1; // Imports the inherited properties of the keys.  
var IMPORT_NODE_ONLY = 2; // Does not import subkeys from the specified file. 
var IMPORT_MERGE = 4; // Merges the imported keys into the existing configuration instead of completely replacing what is there. 

var strPassword = "ExportingPassw0rd"; 
var strFilePath = "C:\\exported.xml"; 
var strSourceMetabasePath = "/lm/logging/custom logging"; // As represented in the metabase.xml file. 
var strDestinationMetabasePath = "/lm/logging/custom logging"; // Can be different from the source. 
var intFlags = IMPORT_NODE_ONLY | IMPORT_INHERITED; // Import only the node with inherited properties. 

// Make connections to WMI, to the IIS namespace on MyMachine, and to the IIsComputer object. 
var locatorObj = new ActiveXObject("WbemScripting.SWbemLocator"); 
var providerObj = locatorObj.ConnectServer("MyMachine", "root/MicrosoftIISv2"); 
var computerObj = providerObj.get("IIsComputer='LM'"); 

// Call export method from the computer object. 
computerObj.Import(strPassword, strFilePath, strSourceMetabasePath, strDestinationMetabasePath, intFlags); 

// Print results. 
WScript.Echo("Imported the node in " + strFilePath + " to " + strDestinationMetabasePath);
4

1 に答える 1

1

.net 4.0:

    dynamic locatorObj = Activator.CreateInstance(Type.GetTypeFromProgID(("WbemScripting.SWbemLocator")));
    dynamic providerObj = locatorObj.ConnectServer("MyMachine", "root/MicrosoftIISv2");  
    dynamic computerObj = providerObj.get("IIsComputer='LM'");

    computerObj.Import(strPassword, strFilePath, strSourceMetabasePath, strDestinationMetabasePath, intFlags);  

.net 3.5以下:

    var locatorObj = Activator.CreateInstance(Type.GetTypeFromProgID(("WbemScripting.SWbemLocator")));
    var providerObj = locatorObj._I("ConnectServer", "MyMachine", "root/MicrosoftIISv2");  
    var computerObj = providerObj._I("get", "IIsComputer='LM'");

    computerObj._I("Import", strPassword, strFilePath, strSourceMetabasePath, strDestinationMetabasePath, intFlags);  


public static class ReflectionHlp
{
  public static object _I(this object item, string name, params object[] args)
  {
    if (item == null)
      return null;
    return item.GetType().InvokeMember(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, item, args);
  }

}
于 2012-07-11T07:38:45.017 に答える