asp.net获取本地IIS上绑定的网站的信息

来源:互联网 发布:程序员技术学习 编辑:程序博客网 时间:2024/04/30 05:45

我们使用ADSI来操作IIS的时候,需要提供他们的Path。比如默认本机80端口的默认站点的目录路径就是:IIS://localhost/w3svc/1/root

它的格式是:
IIS://ComputerName/Service/Website/Directory

ComputerName:即操作的服务器的名字,可以是名字也可以是IP,经常用的就是localhost

Service:即操作的服务器,IIS中有Web,也有FTP,还有SMTP这些服务,我们主要是操作IIS的Web功能,因此此处就是”W3SVC”,如果是FTP则应是”MSFTPSVC”

WebSite:一个IIS服务中可以包括很多的站点,这个就用于设置操作的站点。他的值是一个数字,默认是1,表示缺省站点,如果有其它,也是数字。 
但需要注意的是,并不是自增。后面会有一个小程序获得这个值。

Directory:要操作的目录名称,一个站点一般顶层目录为”ROOT”,其它目录则是他的孩子(Child)。

 

以上资料摘自飞刀的文章,具体看:
http://aspcool.com/lanmu/browse1.asp?ID=914&bbsuser=csharp
http://aspcool.com/lanmu/browse1.asp?ID=915&bbsuser=csharp

由于上面的WebSite 并不是简单的自增。我们要知道某台机子上所有站点对应的值,可以通过下面的小程序获得这个值:

using System.DirectoryServices;
……..

DirectoryEntry root = newDirectoryEntry(“IIS://localhost/W3SVC”);
foreach(DirectoryEntry dir in root.Children)
{
 if(dir.SchemaClassName == “IIsWebServer”)
 {
  string ww =dir.Properties["ServerComment"].Value.ToString();
  Response.Write(string.Format(“IIS://localhost/W3SVC/{0}/ROOT/&nbsp;&nbsp;{1}<br>”,dir.Name,ww));
 }
}

当然,你想获得更多的属性值,可以通过 dir.Properties[] 去获得。

为了说明这里的 WebSite 并不是自增的,下面看我在我本机执行上面程序的结果。

IIS://localhost/W3SVC/1/ROOT/  Default WebSite
IIS://localhost/W3SVC/1307630583/ROOT/ MyWeb81
IIS://localhost/W3SVC/1307630584/ROOT/ MyWeb82
IIS://localhost/W3SVC/1307630585/ROOT/ MyWeb83
IIS://localhost/W3SVC/1307630586/ROOT/ MyWeb84
IIS://localhost/W3SVC/1307630587/ROOT/ MyWeb85
IIS://localhost/W3SVC/1307630683/ROOT/ MyWeb90
IIS://localhost/W3SVC/1307630684/ROOT/ MyWeb91
IIS://localhost/W3SVC/1307630685/ROOT/ MyWeb92
IIS://localhost/W3SVC/1307630686/ROOT/ MyWeb93
IIS://localhost/W3SVC/1758797915/ROOT/ ghj1976.net
IIS://localhost/W3SVC/2/ROOT/  MicrosoftSharePoint Administration
IIS://localhost/W3SVC/2546/ROOT/  94

各个的值都不一样。所以在使用这个路径的时候,不要想当然的以为是简单的自增。



   protected void Page_Load(object sender, EventArgs e)
    {
        DirectoryEntry directoryEntry = new DirectoryEntry(@"IIS://localhost/W3SVC");
        IEnumerator ienum = directoryEntry.Children.GetEnumerator();
        string HostInfo = "";
        while (ienum.MoveNext())
        {
            DirectoryEntry entrypool = (DirectoryEntry)ienum.Current;
            System.DirectoryServices.PropertyCollection ppC = (System.DirectoryServices.PropertyCollection)entrypool.Properties;
            IDictionaryEnumerator idenum = ppC.GetEnumerator();
            if (entrypool.SchemaClassName == "IIsWebServer")
            {
                string[] serverBind = ppC["ServerBindings"][0].ToString().Split(':');//获取网站绑定的IP,端口,主机头
                string EnableDeDoc = ppC["EnableDefaultDoc"][0].ToString();
                string DefaultDoc = ppC["DefaultDoc"][0].ToString();//默认文档
                string MaxConnections = ppC["MaxConnections"][0].ToString();//iis连接数,-1为不限制
                string ConnectionTimeout = ppC["ConnectionTimeout"][0].ToString();//连接超时时间
                string MaxBandwidth = ppC["MaxBandwidth"][0].ToString();//最大绑定数
                string ServerState = ppC["ServerState"][0].ToString();//运行状态                HostInfo += "站点描述:" + ppC["ServerComment"][0].ToString() + "<br>IP地址:" + serverBind[0].ToString() + "<br>TCP端口:" + serverBind[1].ToString() + "<br>主机头:" + serverBind[2].ToString() + "<br>";//获取IIS下所有站点名称
                HostInfo += "启用默认文档:" + EnableDeDoc + "<br>";
                HostInfo += "默认文档:" + DefaultDoc + "<br>";
                HostInfo += "最大连接:" + MaxConnections + "<br>";
                HostInfo += "连接超时:" + ConnectionTimeout + "<br>";
                HostInfo += "最大绑定数:" + MaxBandwidth + "<br>";
                HostInfo += "运行状态:" + ServerState + "<br><br>";
            }
        }
        Response.Write(HostInfo);
        Response.End();
    }

原创粉丝点击