using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.DirectoryServices;
using System.Reflection;
using System.Text.RegularExpressions; int TotalServerCount=; DirectoryEntry rootfolder = new DirectoryEntry("IIS://localhost/W3SVC");
//TotalServerCount=rootfolder.Children.SchemaFilter.Count; foreach (DirectoryEntry child in rootfolder.Children)
{
if (child.SchemaClassName == "IIsWebServer")
{
TotalServerCount+=;
}
}
//循环获取所有站点详细属性写入数组中
string [] arrayServerID = new string[TotalServerCount];//站点标识符
string [] arrayServerIP = new string[TotalServerCount];//站点主机头
string [] arrayServerPort = new string[TotalServerCount];//站点主机头
string [] arrayServerHeader = new string[TotalServerCount];//站点主机头
string [] arrayServerPath = new string[TotalServerCount];//站点主机头
string [] arrayServerComment = new string[TotalServerCount];//站点主机头 string [] arrayServerBinds = new string[TotalServerCount];//站点主机头 string currentServerBindings;//绑定主机头IP端口字符串
char[] a=":".ToCharArray();
string [] currentBingdings =new string[]; int i=;
foreach (DirectoryEntry child in rootfolder.Children)
{
if (child.SchemaClassName == "IIsWebServer")
{
arrayServerID.SetValue(child.Name.ToString(),i);
arrayServerComment.SetValue(child.Properties["ServerComment"].Value.ToString(),i); currentServerBindings=child.Properties["ServerBindings"].Value.ToString(); currentBingdings=currentServerBindings.Split(a); arrayServerIP.SetValue(currentBingdings[],i);
arrayServerPort.SetValue(currentBingdings[],i);
arrayServerHeader.SetValue(currentBingdings[],i); foreach (DirectoryEntry rootChild in child.Children)
{
if((rootChild.SchemaClassName == "IIsWebVirtualDir")&&(rootChild.Name.ToString()=="root"))
{
if(rootChild.Properties["Path"].Value==null)
{
arrayServerPath.SetValue("",i);
}
else
{
arrayServerPath.SetValue(rootChild.Properties["Path"].Value.ToString(),i);
}
}
} i+=;
}
} //写入到datagrid中去
//循环从数组中读取数据
for(i=;i<TotalServerCount;i++)
{ listView1.Items.Add((i+).ToString());
listView1.Items[i].SubItems.Add(arrayServerID.GetValue(i).ToString());
listView1.Items[i].SubItems.Add(arrayServerComment.GetValue(i).ToString());
listView1.Items[i].SubItems.Add(arrayServerIP.GetValue(i).ToString());
listView1.Items[i].SubItems.Add(arrayServerPort.GetValue(i).ToString());
listView1.Items[i].SubItems.Add(arrayServerHeader.GetValue(i).ToString());
listView1.Items[i].SubItems.Add(arrayServerPath.GetValue(i).ToString()); }
public class IISManager
{
string strServer = "localhost";
string strWebSiteID = "";
string strWebSiteName = string.Empty;
string strVirtualPath = string.Empty; public string Server
{
get { return strServer; }
set { strServer = value; }
}
public string WebSiteID
{
get { return strWebSiteID; }
set { strWebSiteID = value; }
}
public string WebSiteName
{
get { return strWebSiteName; }
set { strWebSiteName = value; }
}
public string VirtualPath
{
get { return strVirtualPath; }
set { strVirtualPath = value; }
} public IISManager()
{
} public IISManager(string strS, string strW)
{
strServer = strS;
strWebSiteID = strW;
} public string GetConfigAllFilePath()
{
DirectoryEntry rootEntry = new DirectoryEntry("IIS://" + strServer + "/w3svc/" + strWebSiteID + "/root"); string strTempPath = string.Empty;
foreach (DirectoryEntry de in rootEntry.Children)
{
if (de.Name == "kintera_com")
{
strTempPath = de.Properties["path"][].ToString();
break;
}
}
strTempPath = strTempPath.Substring(, strTempPath.Length - );
return strTempPath + @"\CommonLib\includes\INC_config_all.asp";
} public void CreateWebSite()
{
DirectoryEntry root = new DirectoryEntry("IIS://" + this.Server + "/W3SVC"); if (!EnsureNewSiteAvaible(this.Server))
{
throw(new Exception("The Web Site existed!"));
}
else
{
DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", this.WebSiteID); site.Invoke("Put", "ServerComment", this.WebSiteName);
site.Invoke("Put", "KeyType", "IIsWebServer");
site.Invoke("Put", "ServerBindings", this.Server);
site.Invoke("Put", "ServerState", );
site.Invoke("Put", "FrontPageWeb", );
site.Invoke("Put", "DefaultDoc", "index.aspx,index.html,index.html,default.aspx,default.htm,default.html");
site.Invoke("Put", "ServerAutoStart", );
site.Invoke("Put", "ServerSize", );
site.Invoke("SetInfo");
}
} public void CreateVirtualFolder()
{
DirectoryEntry site = new DirectoryEntry("IIS://" + this.Server + "/W3SVC" + this.WebSiteID + "/root"); DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir"); siteVDir.Properties["AppIsolated"][] = ; siteVDir.Properties["Path"][] = this.VirtualPath; siteVDir.Properties["AccessFlags"][] = ; siteVDir.Properties["FrontPageWeb"][] = ; siteVDir.Properties["AppRoot"][] = "/W3SVC/" + this.WebSiteID + "/Root"; siteVDir.Properties["AppFriendlyName"][] = "ROOT"; siteVDir.CommitChanges(); site.CommitChanges();
} public bool EnsureNewSiteAvaible(string bindStr)
{
string strDePath = String.Format("IIS://{0}/w3svc", this.Server); DirectoryEntry de = new DirectoryEntry(strDePath);
foreach (DirectoryEntry child in de.Children)
{
if (child.SchemaClassName == "IIsWebServer")
{
if (child.Properties["ServerBindings"].Value != null)
{
if (child.Properties["ServerBindings"].Value.ToString() == bindStr)
{
return false;
}
}
}
}
return true;
} public void DeleteWebSiteByName(string siteName)
{
string siteNum = GetWebSiteNum(siteName); string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", this.Server, siteNum); DirectoryEntry siteEntry = new DirectoryEntry(siteEntPath); string rootPath = String.Format("IIS://{0}/w3svc", this.Server); DirectoryEntry rootEntry = new DirectoryEntry(rootPath); rootEntry.Children.Remove(siteEntry); rootEntry.CommitChanges();
} public void DeleteWebSiteByName()
{
string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", this.Server,this.WebSiteID); DirectoryEntry siteEntry = new DirectoryEntry(siteEntPath); string rootPath = String.Format("IIS://{0}/w3svc", this.Server); DirectoryEntry rootEntry = new DirectoryEntry(rootPath); rootEntry.Children.Remove(siteEntry); rootEntry.CommitChanges();
} public void StartWebSite(string siteName)
{
string siteNum = GetWebSiteNum(siteName); string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", this.Server, siteNum); DirectoryEntry siteEntry = new DirectoryEntry(siteEntPath); siteEntry.Invoke("Start", new object[] { });
} public void StartWebSite()
{
string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", this.Server,this.WebSiteID); DirectoryEntry siteEntry = new DirectoryEntry(siteEntPath); siteEntry.Invoke("Start", new object[] { });
} public void StopWebSite(string siteName)
{
string siteNum = GetWebSiteNum(siteName); string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", this.Server, siteNum); DirectoryEntry siteEntry = new DirectoryEntry(siteEntPath); siteEntry.Invoke("Stop", new object[] { });
} public void StopWebSite()
{
string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", this.Server, this.WebSiteID); DirectoryEntry siteEntry = new DirectoryEntry(siteEntPath); siteEntry.Invoke("Stop", new object[] { });
} public string GetWebSiteNum(string siteName)
{
Regex regex = new Regex(siteName);
string tmpStr;
string rootPath = String.Format("IIS://{0}/w3svc", this.Server); DirectoryEntry deEntry = new DirectoryEntry(rootPath); foreach (DirectoryEntry child in deEntry.Children)
{
if (child.SchemaClassName == "IIsWebServer")
{
if (child.Properties["ServerBindings"].Value != null)
{
tmpStr = child.Properties["ServerBindings"].Value.ToString(); if (regex.Match(tmpStr).Success)
{
return child.Name;
}
} if (child.Properties["ServerComment"].Value != null)
{
tmpStr = child.Properties["ServerComment"].Value.ToString(); if (regex.Match(tmpStr).Success)
{
return child.Name;
}
}
}
}
return "No WebSite";
} public void ResetIIS()
{
Process.Start("iisreset");
} public void StopIIS()
{
ServiceController sc = new ServiceController("iisadmin");
if (sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
}
//Process.Start("cmd.exe", "/start iisreset");
} public void StartIIS()
{
ServiceController sc = new ServiceController("iisadmin");
sc.Start();
}
}
public List<string> EnumWebSite()
{
string strDePath = String.Format("IIS://{0}/w3svc", this.Server);
DirectoryEntry de = new DirectoryEntry(strDePath);
List<string> list=new List<string>();
foreach (DirectoryEntry child in de.Children)
{
if (child.SchemaClassName == "IIsWebServer")
{
list.Add(child.Properties["ServerComment"].Value.ToString());
}
}
}
            // C# 获取网站的 IIS 站点名称 ,获取站点当前连接数
string siteName = System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName();
System.Management.ManagementObject o = new System.Management.ManagementObject("Win32_PerfFormattedData_W3SVC_WebService.Name=siteName");
Response.Write(o.Properties["CurrentConnections"].Value.ToString());
<p>服务器IP:<%=Request.ServerVariables["LOCAL_ADDR"]%></p>
<p>服务器名:<%=Request.ServerVariables["SERVER_NAME"]%></p>
<p> HTTP端口:<%=Request.ServerVariables["SERVER_PORT"]%></p>
<p> 服务器时间:<%=DateTime .Now%></p>
<p> 操作系统信息:<%=Request.ServerVariables["HTTP_USER_AGENT"]%></p>
<p> 允许文件:<%=Request.ServerVariables["HTTP_ACCEPT"]%></p>
<p> 虚拟目录:<%=HttpContext.Current.Request.ApplicationPath%></p>
<p> 物理路径:<%=HttpRuntime.AppDomainAppPath%></p>
<p> 探针文件路径:<%=Context.Server.MapPath(Request.ServerVariables["SCRIPT_NAME"])%></p>
<p> 脚本超时时间:<%=Server.ScriptTimeout%>(秒)</p>
<p> CPU个数: <%=Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS")%></p> String serverOS = Environment.OSVersion.ToString();
String CpuSum = Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS");// CPU个数:
String CpuType = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER");// CPU类型:
String ServerSoft = Request.ServerVariables["SERVER_SOFTWARE"]; // 信息服务软件:
String MachineName = Server.MachineName;// 服务器名
String ServerName = Request.ServerVariables["SERVER_NAME"];// 服务器域名
String ServerPath = Request.ServerVariables["APPL_PHYSICAL_PATH"];// 虚拟服务绝对路径
String ServerNet = ".NET CLR " + Environment.Version.ToString(); // DotNET 版本
String ServerArea = (DateTime.Now - DateTime.UtcNow).TotalHours > ? "+" + (DateTime.Now -DateTime.UtcNow).TotalHours.ToString() : (DateTime.Now - DateTime.UtcNow).TotalHours.ToString();// 服务器时区
String ServerTimeOut = Server.ScriptTimeout.ToString(); // 脚本超时时间
String ServerStart = ((Double)System.Environment.TickCount / ).ToString("N2");// 开机运行时长
// AspNet CPU时间
String ServerSessions = Session.Contents.Count.ToString();// Session总数
String ServerApp = Application.Contents.Count.ToString(); // Application总数
String ServerCache = Cache.Count.ToString(); //应用程序缓存总数
// 应用程序占用内存
// String ServerFso = Check("Scripting.FileSystemObject"); // FSO 文本文件读写
String ServerTimeOut = Server.ScriptTimeout.ToString() + "毫秒"; // 本页执行时间 HTTP_USER_AGENT 获得用户使用的浏览器类型和版本 REMOTE_ADDR 获取用户的IP地址 REQUEST_METHOD 获取请求的方法 LOCAL_ADDR 获取服务器IP地址 SERVER_NAME 获取服务器主机名 PATH_INFO 获取当前执行程序的虚拟路径 PATH_TRANSLATED 获取当前执行程序的绝对路径 CONTENT_LENGTH 获取请求程序所发送内容的字符总数 CONTENT_TYPE 获取请求的信息类型 GATEWAY_INTERFACE 获取网关接口 QUERY_STRING 获取URL的附加信息 SCRIPT_NAME 获取当前程序的文件名(包含虚拟路径) SERVER_PORT 获取服务器接受请求的端口 SERVER_PROTOCOL 获取服务器遵从的协议以及版本号 HTTP_ACCEPT_LANGUAGE 获取用户所使用的语言 using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; public partial class AdminLogin_Default : System.Web.UI.Page
{
#region 必需的设计器变量
protected string MemberName, AdminLevel;
protected string ServerOS, CpuSum, CpuType, ServerSoft, MachineName, ServerName, ServerPath, ServerNet, ServerArea, ServerTimeOut, ServerStart;
protected string PrStart, AspNetN, AspNetCpu, ServerSessions, ServerApp, ServerCache, ServerAppN, ServerFso, RunTime;
#endregion protected void Page_Load(object sender, EventArgs e)
{
DataLoad();
} #region 获取服务器及用户信息
private void DataLoad()
{
DateTime sTime = DateTime.Now; ServerOS = Environment.OSVersion.ToString(); //操作系统:
CpuSum = Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS"); //CPU个数:
CpuType = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER"); //CPU类型:
ServerSoft = Request.ServerVariables["SERVER_SOFTWARE"]; //信息服务软件:
MachineName = Server.MachineName; //服务器名
ServerName = Request.ServerVariables["SERVER_NAME"]; //服务器域名
ServerPath = Request.ServerVariables["APPL_PHYSICAL_PATH"]; //虚拟服务绝对路径
ServerNet = ".NET CLR " + Environment.Version.ToString(); //DotNET 版本
ServerArea = (DateTime.Now - DateTime.UtcNow).TotalHours > ? "+" + (DateTime.Now - DateTime.UtcNow).TotalHours.ToString() : (DateTime.Now - DateTime.UtcNow).TotalHours.ToString(); //服务器时区
ServerTimeOut = Server.ScriptTimeout.ToString(); //脚本超时时间
ServerStart = ((Double)System.Environment.TickCount / ).ToString("N2"); //开机运行时长
PrStart = GetPrStart(); //进程开始时间
AspNetN = GetAspNetN(); //AspNet 内存占用
AspNetCpu = GetAspNetCpu(); //AspNet CPU时间
ServerSessions = Session.Contents.Count.ToString(); //Session总数
ServerApp = Application.Contents.Count.ToString(); //Application总数
ServerCache = Cache.Count.ToString(); //应用程序缓存总数
ServerAppN = GetServerAppN(); //应用程序占用内存
ServerFso = Check("Scripting.FileSystemObject"); //FSO 文本文件读写
ServerTimeOut = Server.ScriptTimeout.ToString() + "毫秒"; //本页执行时间
if (ServerSoft == "")
{
ServerSoft = "无";
} #region 页面初始化必需的内容
Header.Title = "沉紫龙" + " - 系统管理";
//HeaderTextInfo.Initialization();
//HeaderTextInfo.GetAdminHeader(this, null);
#endregion #region WebControls Config
// ViewState Config
this.EnableViewState = false;
#endregion //执行时间
DateTime eTime = DateTime.Now;
RunTime = ((eTime - sTime).TotalMilliseconds).ToString();
}
#endregion #region 获取服务器系统信息
private string GetServerAppN()
{
string temp;
try
{
temp = ((Double)GC.GetTotalMemory(false) / ).ToString("N2") + "M";
}
catch
{
temp = "未知";
}
return temp;
} private string GetAspNetN()
{
string temp;
try
{
temp = ((Double)System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 / ).ToString("N2") + "M";
}
catch
{
temp = "未知";
}
return temp;
} private string GetAspNetCpu()
{
string temp;
try
{
temp = ((TimeSpan)System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime).TotalSeconds.ToString("N0");
}
catch
{
temp = "未知";
}
return temp;
} private string GetPrStart()
{
string temp;
try
{
temp = System.Diagnostics.Process.GetCurrentProcess().StartTime.ToString();
}
catch
{
temp = "未知";
}
return temp;
} private string Check(string obj)
{
try
{
object claobj = Server.CreateObject(obj);
return "支持";
}
catch
{
return "不支持";
}
}
#endregion
}

C#列表所有IIS站点以及相关站点属性的更多相关文章

  1. VS2013 IIS Express 无法显示站点目录列表

      VS2013 默认使用IIS Express作为虚拟服务器,或许出于与安全考虑该版本的IIS Express默认关闭站点目录列表显示的功能,访问时会出现以下错误:   为了方便起见,我使用了全局设 ...

  2. 通过Jenkins在IIS上布署站点

    当需要在多台服务器的IIS上布署站点时,如果纯粹靠人工手动完成此任务的话,过于低效,而借助Jenkins之类的自动化工具,则可以极大提升工作效率. 以下便是Jenkins Pipeline所使用的脚本 ...

  3. 在 IIS 上创建 FTP 站点

    微软参考文档: 在 IIS 上生成 FTP 站点 主要过程: 1.控制面板 -> 程序 -> 启动或关闭Windows功能 -> 将Internet Information Serv ...

  4. Win8下IIS的安装和站点的公布

    版权声明:本文为博主原创文章,不经博主同意注明链接就可以转载. https://blog.csdn.net/Senior_lee/article/details/32939411         之前 ...

  5. 在IIS上搭建FTP站点

    操作环境 系统:win7 IIS版本:7.5 FTP传输工具:FlashXP 概述 本文介绍了如何在win7下利用IIS(默认已安装IIS和FTP功能)搭建FTP站点,FTP站点的常用配置. 快速搭建 ...

  6. IIS的ARR实现站点的负载均衡 nginx 对比

    windows下使用IIS的ARR实现站点的负载均衡 - CSDN博客 https://blog.csdn.net/zzy7075/article/details/73294713 IIS的ARR实现 ...

  7. IIS 虚拟目录 VS 站点

    1.虚拟目录是站点的一个子目录,虚拟目录是建立在站点下面的. 2.一个站点下面,可以建立多个虚拟目录.各个虚拟目录都是以虚拟目录名称来进行区分. 3.新建的独立站点就和IIS中的默认站点一样,各个站点 ...

  8. Http站点转Https站点教程

    https://blog.csdn.net/tanga842428/article/details/79273226 Http站点转Https站点教程 2018年02月28日 12:04:35 坦GA ...

  9. 【.NET特供-第三季】ASP.NET MVC系列:传统WebForm站点和MVC站点执行机制对照

    本文以图形化的方式,从'执行机制'方面对照传统WebForm站点和MVC站点. 请參看下面图形: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb2 ...

随机推荐

  1. EZ 2018 04 01 ZJOI2018模拟赛04.01

    现在开始填以前的坑 这次老叶强制我们打一下这次省选题,然后我已经做好了掉Rating到死的准备 然后考完--莫名涨了 Orz 题目链接 由于很多东西我都不会,所以详细请看:dalao的题解 T1 我T ...

  2. adb连接不上手机的解决方案

    一.确认手机的USB调试接口是打开的:----------打开开发者模式,暴击手机版本号多次,直到提示已打开开发者模式. 二.使用USB线连接电脑和手机,可以首先执行adb remount(重新挂载系 ...

  3. QTableWidget 详细使用

    QTableWidget 详细使用

  4. 51nod 小朋友的笑话

    链接 分析: 每次操作把以前没有出现这个数的设为1,有这个数的设为0.首先将当前区间设为1,考虑有set维护这个颜色出现的区间,然后把所有与当前区间相交的拿出来,修改为0. 复杂度?每次操作的线段只会 ...

  5. Jenkins 构建运行java程序

    我们将在Jenkins建立执行一个简单的 HelloWorld 应用程序,构建和运行Java程序.打开网址:http://localhost:8080/jenkins 第1步- 转到Jenkins 仪 ...

  6. proe工程图输出dwg/dxf文件设置

    网上看到不少人分享proe转转dxf/dwg配置文件的,但是看了一圈,几乎都没有涉及到转化线型的,所以自己整理自己的配置文件,写在这里分享出来. 以Pro/engineer WF5.0为例: 1.复制 ...

  7. js闭包的作用

    js闭包的用途详解 js闭包可以用在许多地方.它的最大用处有两个,一个是前面提到的可以读取函数内部的变量,另一个就是让这些变量的值始终保持在内存中.具体怎么理解呢,各位看官请仔细看好下文   我们来看 ...

  8. Final发布 文案+美工展示

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2476项目地址:https://coding.net/u/wuyy694/ ...

  9. 12.16daily_scrum

    这个阶段,我们组需要攻克的技术难题一个是测试及美化界面,另一个是在M1阶段的基础上进一步细化和完善悬浮窗的功能,具体的工作内容如下: 具体工作: 小组成员 今日任务 明日任务 工作时间 李睿琦 图片笔 ...

  10. 冲刺Two之站立会议2

    今天我们进行了主界面部分的设置,因为它包含的部分有很多,所以就只能它拆分进行一一突破.今天主要完成了主界面的框架搭建,以及添加了需要的按钮,包括好友管理,退出登录,开启聊天通信界面的内容等.