Get IIS site name from for an ASP.NET website

后端 未结 6 1263
长发绾君心
长发绾君心 2021-02-02 05:59

In my ASP.NET web app I\'d like to look up the name it was given when it was created in IIS, which is unique to the server. I\'d not interested in the domain name for the web si

6条回答
  •  旧时难觅i
    2021-02-02 06:10

    Here is a related post in retrieving the site Id.

    Here some code that might work for you:

    using System.DirectoryServices;
    using System;
    
    public class IISAdmin
    {
       public static void GetWebsiteID(string websiteName)
       {
          DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc");
    
         foreach(DirectoryEntry de in w3svc.Children)
         {
            if(de.SchemaClassName == "IIsWebServer" && de.Properties["ServerComment"][0].ToString() == websiteName)
            {
               Console.Write(de.Name);
            }
    
         }
    
      }
      public static void Main()
      {
         GetWebsiteID("Default Web Site");
      }
    

    }

    Here's the link to the original post.

    I'm not sure if it will work on IIS7, but if you install the IIS6 compatibility components for IIS7 it should work.

提交回复
热议问题