简体   繁体   中英

An Application which fetches IIS and App Pool details of website hosted in remote server

I got a task of creating an Web application in C# ,which fetches IIS and App Pool details of Website hosted in the remote server(same location).Any idea or help is well appreciated!!!

-Renji

This is somehow a broad question, but in order to help here are some points you can start from:

  1. get the website name at IIS: System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName()
  2. to get the list of websites and virtual dirs, check this: How to programmatically get sites list and virtual dirs in IIS 7?
  3. to manage IIS: http://www.codeproject.com/Articles/99634/Use-C-to-manage-IIS
  4. get IIS version: How to detect IIS version using C#?
  5. get site status: Programmatically get site status from IIS, gets back COM error

I guess this is enough for you to start exploring everything related to IIS, hope it helps.

I was also having the same requirement. On doing so many trail and error approach I got the result.

Pre-Requisites

  • IIS 6 and Above
  • .Net Framework 4.0 and Above
  • Add reference to -System.DirectoryServices and Microsoft.Web.Administration

c# Console Application Method

public static SortedDictionary<string,string> GetApplicationPoolNames ( string mname = null )
    {
        try
        {
            ServerManager manager = new ServerManager ();
            SortedDictionary<string,string> ApplicationPoolStatus = new SortedDictionary<string,string> ();
            if (string.IsNullOrEmpty (mname))
                mname = System.Environment.MachineName;
            string appPoolName = string.Empty;
            manager = ServerManager.OpenRemote (mname);

            ApplicationPoolCollection applicationPoolCollection = manager.ApplicationPools;

            foreach (ApplicationPool applicationPool in applicationPoolCollection)
            {
                if (!string.IsNullOrEmpty (applicationPool.Name))
                {
                    if (!ApplicationPoolStatus.ContainsKey (applicationPool.Name))
                    {
                        ApplicationPoolStatus.Add (applicationPool.Name,string.Empty);
                    }
                    ApplicationPoolStatus[applicationPool.Name] = applicationPool.State.ToString ();
                }
            }
            return ApplicationPoolStatus;
        }
        catch (Exception)
        {
            throw;
        }
    }

ASP.Net Web/MVC Application Method

public SortedDictionary<string,string> ShowApplicationPoolDatas ()
    {
        SortedDictionary<string,string> ApplicationPoolStatus = new SortedDictionary<string,string> ();
        var domain = this.HttpContext.Request.Url.Host;
        DirectoryEntry Services = new DirectoryEntry ("IIS://"+ domain + "/W3SVC/APPPOOLS");
        foreach (DirectoryEntry Entry in Services.Children)
        {
            if (!string.IsNullOrEmpty (Entry.Name))
            {
                if (!ApplicationPoolStatus.ContainsKey (Entry.Name))
                {
                    ApplicationPoolStatus.Add (Entry.Name,string.Empty);
                }
            }
            var intStatus = (Int32)Entry.InvokeGet ("AppPoolState");
            switch (intStatus)
            {
                case 2:
                    ApplicationPoolStatus[Entry.Name] = "Running";
                    break;
                case 4:
                    ApplicationPoolStatus[Entry.Name] = "Stopped";
                    break;
                default:
                    ApplicationPoolStatus[Entry.Name] = "Unknown";
                    break;
            }
        }
        return ApplicationPoolStatus;

    }

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