C#列表所有IIS站点以及相关站点属性
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站点以及相关站点属性的更多相关文章
- VS2013 IIS Express 无法显示站点目录列表
VS2013 默认使用IIS Express作为虚拟服务器,或许出于与安全考虑该版本的IIS Express默认关闭站点目录列表显示的功能,访问时会出现以下错误: 为了方便起见,我使用了全局设 ...
- 通过Jenkins在IIS上布署站点
当需要在多台服务器的IIS上布署站点时,如果纯粹靠人工手动完成此任务的话,过于低效,而借助Jenkins之类的自动化工具,则可以极大提升工作效率. 以下便是Jenkins Pipeline所使用的脚本 ...
- 在 IIS 上创建 FTP 站点
微软参考文档: 在 IIS 上生成 FTP 站点 主要过程: 1.控制面板 -> 程序 -> 启动或关闭Windows功能 -> 将Internet Information Serv ...
- Win8下IIS的安装和站点的公布
版权声明:本文为博主原创文章,不经博主同意注明链接就可以转载. https://blog.csdn.net/Senior_lee/article/details/32939411 之前 ...
- 在IIS上搭建FTP站点
操作环境 系统:win7 IIS版本:7.5 FTP传输工具:FlashXP 概述 本文介绍了如何在win7下利用IIS(默认已安装IIS和FTP功能)搭建FTP站点,FTP站点的常用配置. 快速搭建 ...
- IIS的ARR实现站点的负载均衡 nginx 对比
windows下使用IIS的ARR实现站点的负载均衡 - CSDN博客 https://blog.csdn.net/zzy7075/article/details/73294713 IIS的ARR实现 ...
- IIS 虚拟目录 VS 站点
1.虚拟目录是站点的一个子目录,虚拟目录是建立在站点下面的. 2.一个站点下面,可以建立多个虚拟目录.各个虚拟目录都是以虚拟目录名称来进行区分. 3.新建的独立站点就和IIS中的默认站点一样,各个站点 ...
- Http站点转Https站点教程
https://blog.csdn.net/tanga842428/article/details/79273226 Http站点转Https站点教程 2018年02月28日 12:04:35 坦GA ...
- 【.NET特供-第三季】ASP.NET MVC系列:传统WebForm站点和MVC站点执行机制对照
本文以图形化的方式,从'执行机制'方面对照传统WebForm站点和MVC站点. 请參看下面图形: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb2 ...
随机推荐
- # 20155337《网络对抗》Web基础
20155337<网络对抗>Exp8 Web基础 实践目标 1. 实践内容 (1).Web前端HTML 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写 ...
- TreeSet排序相关总结
java的集合这一块在工作中用得还比较多,有些东西老是忘,因此在此记录下来. TreeSet原理 1.特点 TreeSet是用来排序的, 可以指定一个顺序, 对象存入之后会按照指定的顺 ...
- EZ 2018 05 13 NOIP2018 模拟赛(十三)
这次的比赛真心水,考时估分240,然后各种悠闲乱逛 然后测完T1数组开小了炸成40,T2,T3都没开long long,T2炸成20,T3爆0 掉回1600+的深渊,但是还有CJJ dalao比我更惨 ...
- 2014.9.11 Research Meeting Report
Dear All: Yesterday when we read INFOCOM papers, you have seen how damage it is to have careless wri ...
- PAT甲题题解-1050. String Subtraction (20)-水题
#include <iostream> #include <cstdio> #include <string.h> #include <algorithm&g ...
- PAT甲题题解-1096. Consecutive Factors(20)-(枚举)
题意:一个正整数n可以分解成一系列因子的乘积,其中会存在连续的因子相乘,如630=3*5*6*7,5*6*7即为连续的因子.给定n,让你求最大的连续因子个数,并且输出其中最小的连续序列. 比如一个数可 ...
- 第二阶段冲刺——five
个人任务: 王金萱:合并程序(统计团队博客). 季方:合并并排除运行团队博客时出现的错误. 马佳慧:优化登录.注册信息的填写判断. 司宇航:完成打印名单的功能. 站立会议: 任务看板和燃尽图:
- 哥java学识有大进 干回之前的小学生系统像切菜一样简单 不说了 来代码
package runok; import java.util.*; import java.awt.*; import java.awt.event.ActionEvent; import java ...
- 第一个spring,总结!
陈志棚:界面跳转与框架 李天麟:游戏界面ui 徐侃:算法代码的设计 经过五天的时间,开会时候分配的任务,已经全部完成,在这期间中,我们遇到了一些问题.但是,经过android老师的指导后,app成功的 ...
- Helm 安装 nginx-ingress 的方法
还是没学会 今天继续研究. Using Helm NGINX Ingress controller can be installed via Helm using the chart stable/n ...