Pregunta

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
¿Fue útil?

Solución

I think you can use following code to find what you need. In case other questions just use the same approach. This code works for 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));
                        }
                    }
                }
            }
        }
    }
}

With regards to IIS 7 I wrote this code:

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));
                    }
                }
            }
        }
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top