简体   繁体   中英

WMI to retrieve website physical path in c#

I've created this VBScript WMI script:

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

Which returns the physical path ( C:\\inetpub\\wwwroot\\webapplication1 ) to all the applications in IIS.

Now I'm trying to use C# to populate a combobox with those values:

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;
}

But the output is the virtual path:

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

What needs to be changed in my query?

Note: I need to support IIS6 and .NET 4.0

Finally got it...

ManagementObjectSearcher searcher =
   new ManagementObjectSearcher("root\\MicrosoftIISv2", 
                                "SELECT * FROM IIsWebVirtualDirSetting");

foreach (ManagementObject queryObj in searcher.Get())
{
   result.Add(queryObj["Path"]);
}

I prefer like this:

Connect at my local network server 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM