【问题标题】:WMI to retrieve website physical path in c#WMI在c#中检索网站物理路径
【发布时间】:2013-09-01 22:35:01
【问题描述】:

我已经创建了这个 VBScript WMI 脚本:

On Error Resume Next

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Set objWMIService = GetObject("winmgmts:\\localhost\root\MicrosoftIISv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM IIsWebVirtualDirSetting", _
                 "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems

   WScript.Echo "Path: " & objItem.Path
   WScript.Echo
Next

返回 IIS 中所有应用程序的物理路径 (C:\inetpub\wwwroot\webapplication1)。

现在我正在尝试使用 C# 用这些值填充组合框:

public static ArrayList Test2()
{
    ArrayList WebSiteListArray = new ArrayList();

    ConnectionOptions connection = new ConnectionOptions();
    ManagementScope scope = 
        new ManagementScope(@"\\" + "localhost" + @"\root\MicrosoftIISV2", 
                            connection);
    scope.Connect();

    ManagementObjectSearcher searcher = 
        new ManagementObjectSearcher(scope, 
                new ObjectQuery("SELECT * FROM IIsWebVirtualDirSetting"), null);

    ManagementObjectCollection webSites = searcher.Get();
    foreach (ManagementObject webSite in webSites)
    {
        WebSiteListArray.Add(webSite.Path);
    }            

    return WebSiteListArray;
}

但输出的是虚拟路径:

(`IIsWebVirtualDirSetting.Name="W3SVC/1/ROOT/webapplication1"`)

我的查询需要改变什么?

注意:我需要支持 IIS6 和 .NET 4.0

【问题讨论】:

    标签: c# iis vbscript path wmi


    【解决方案1】:

    终于明白了……

    ManagementObjectSearcher searcher =
       new ManagementObjectSearcher("root\\MicrosoftIISv2", 
                                    "SELECT * FROM IIsWebVirtualDirSetting");
    
    foreach (ManagementObject queryObj in searcher.Get())
    {
       result.Add(queryObj["Path"]);
    }
    

    【讨论】:

    • 这是从特定网站检索的吗?
    【解决方案2】:

    我更喜欢这样:

    连接到我的本地网络服务器SOMEREMOTESERVER:

    ConnectionOptions connection = new ConnectionOptions();
    connection.Authentication = System.Management.AuthenticationLevel.PacketPrivacy;
    ManagementScope scope =
        new ManagementScope(@"\\SOMEREMOTESERVER\root\MicrosoftIISV2",
                            connection);
    scope.Connect();
    
    ObjectQuery query = new ObjectQuery("SELECT * FROM IISWebServerSetting");
    
    var collection = new ManagementObjectSearcher(scope, query).Get();
    
    foreach (ManagementObject item in collection)
    {
        var value = item.Properties["ServerBindings"].Value;
    
        if (value is Array)
        {
            foreach (ManagementBaseObject a in value as Array)
            {
                Console.WriteLine(a["Hostname"]);
            }
        }
    
        ManagementObject maObjPath = new ManagementObject(item.Scope,
        new ManagementPath(
        string.Format("IISWebVirtualDirSetting='{0}/root'", item["Name"])),
        null);
    
        PropertyDataCollection properties = maObjPath.Properties;
        Console.WriteLine(properties["path"].Value);
    
        Console.WriteLine(item["ServerComment"]);
        Console.WriteLine(item["Name"]);
    
        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多