0

I am getting all websites from localhost IIS manager 6 using DirectoryEntry class, I would like to get local path of each web application, but not sure how to get it, Is there anyway I can enumerate all properties of directory entry ?

DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");

foreach (DirectoryEntry e in root.Children)
{
    if (e.SchemaClassName == "IIsWebServer")
    {
        Console.WriteLine(e.Properties["ServerComment"].Value.ToString());
        // how can I enumerate all properties and there values here ? 
        // maybe write to a xml document to find the local path property
4

1 回答 1

1

我认为您可以使用以下代码找到所需的内容。如果其他问题只是使用相同的方法。此代码适用于 IIS6。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



using System.DirectoryServices;

namespace IIS_6
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
            string VirDirSchemaName = "IIsWebVirtualDir";

            foreach (DirectoryEntry e in root.Children)
            {

                foreach (DirectoryEntry folderRoot in e.Children)
                {
                    foreach (DirectoryEntry virtualDirectory in folderRoot.Children)
                    {
                        if (VirDirSchemaName == virtualDirectory.SchemaClassName)
                        {
                            Console.WriteLine(String.Format("\t\t{0} \t\t{1}", virtualDirectory.Name, virtualDirectory.Properties["Path"].Value));
                        }
                    }
                }
            }
        }
    }
}

关于 IIS 7,我编写了以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// to add it from %windir%\System32\InetSrv\Microsoft.Web.Administration.dll
using Microsoft.Web.Administration;

namespace IIS_7
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServerManager serverManager = new ServerManager())
            {
                foreach (var site in serverManager.Sites)
                {

                    Console.WriteLine(String.Format("Site: {0}", site.Name));

                    foreach (var app in site.Applications)
                    {
                        var virtualRoot = app.VirtualDirectories.Where(v => v.Path == "/").Single();

                        Console.WriteLine(String.Format("\t\t{0} \t\t{1}", app.Path, virtualRoot.PhysicalPath));
                    }
                }
            }
        }
    }
}
于 2013-10-23T14:40:57.520 回答