how to get the application pool name for a specific website IIS6 programmatically? C#

前端 未结 3 1897
小鲜肉
小鲜肉 2021-01-06 11:14

how to get the application pool name for a specific website IIS 6 programmatic using C#

EDIT: I already used the methods of DirectoryServices namespace but the appl

3条回答
  •  离开以前
    2021-01-06 12:05

    I don't agree with you. I coded up a test app and I get the correct AppPool name from it, even if I set the AppPool manually using IIS Manager.

    To make sure, I have tested once, name name was ok; then, I popep up the IIS Manager, changed the AppPool, executed iisreset, and ran the test app again - the AppPool name I got was correct again. I don't how your code looked like, but mine is like this:

    using System;
    using System.IO;
    using System.DirectoryServices;
    
    class Class
    {
        static void Main(string[] args)
        {
            DirectoryEntry entry = FindVirtualDirectory("", "Default Web Site", "");
            if (entry != null)
            {
                Console.WriteLine(entry.Properties["AppPoolId"].Value);
            }
        }
    
        static DirectoryEntry FindVirtualDirectory(string server, string website, string virtualdir)
        {
            DirectoryEntry siteEntry = null;
            DirectoryEntry rootEntry = null;
            try
            {
                siteEntry = FindWebSite(server, website);
                if (siteEntry == null)
                {
                    return null;
                }
    
                rootEntry = siteEntry.Children.Find("ROOT", "IIsWebVirtualDir");
                if (rootEntry == null)
                {
                    return null;
    
                }
    
                return rootEntry.Children.Find(virtualdir, "IIsWebVirtualDir");
            }
            catch (DirectoryNotFoundException ex)
            {
                return null;
            }
            finally
            {
                if (siteEntry != null) siteEntry.Dispose();
                if (rootEntry != null) rootEntry.Dispose();
            }
        }
    
        static DirectoryEntry FindWebSite(string server, string friendlyName)
        {
            string path = String.Format("IIS://{0}/W3SVC", server);
    
            using (DirectoryEntry w3svc = new DirectoryEntry(path))
            {
                foreach (DirectoryEntry entry in w3svc.Children)
                {
                    if (entry.SchemaClassName == "IIsWebServer" &&
                        entry.Properties["ServerComment"].Value.Equals(friendlyName))
                    {
                        return entry;
                    }
                }
            }
            return null;
        }
    }
    

    Sorry for my lousy english.
    Hope I've helped.

提交回复
热议问题