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

拜拜、爱过 提交于 2019-12-25 15:33:43

问题


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


回答1:


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.




回答2:


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;

    }


来源:https://stackoverflow.com/questions/18349532/an-application-which-fetches-iis-and-app-pool-details-of-website-hosted-in-remot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!