C#操作IIS类
1
using System;2
using System.DirectoryServices;3
using System.Collections;4
using System.Text.RegularExpressions;5
using System.Text;6

7
namespace Business8


{9
public class ControlIIS10

{11

UserName,Password,HostName的定义#region UserName,Password,HostName的定义12
public static string HostName13

{14
get15

{16
return hostName;17
}18
set19

{20
hostName = value;21
}22
}23
public static string UserName24

{25
get26

{27
return userName;28
}29
set30

{31
userName = value;32
}33
}34
public static string Password35

{36
get37

{38
return password;39
}40
set41

{42
if (UserName.Length <= 1)43

{44
throw new ArgumentException("还没有指定好用户名。请先指定用户名");45
}46
password = value;47
}48
}49
public static void RemoteConfig(string hostName, string userName, string password)50

{51
HostName = hostName;52
UserName = userName;53
Password = password;54
}55
private static string hostName = "localhost";56
private static string userName = "qf";57
private static string password = "qinfei";58
#endregion59

60

根据路径构造Entry的方法#region 根据路径构造Entry的方法61

/**//// <summary>62
/// 根据是否有用户名来判断是否是远程服务器。63
/// 然后再构造出不同的DirectoryEntry出来64
/// </summary>65
/// <param name="entPath">DirectoryEntry的路径</param>66
/// <returns>返回的是DirectoryEntry实例</returns>67
public static DirectoryEntry GetDirectoryEntry(string entPath)68

{69
DirectoryEntry ent;70
if (UserName == null)71

{72
ent = new DirectoryEntry(entPath);73
}74
else75

{76
ent = new DirectoryEntry(entPath, HostName + "\\" + UserName, Password, AuthenticationTypes.Secure);77
//ent = new DirectoryEntry(entPath, UserName, Password, AuthenticationTypes.Secure);78
}79
return ent;80
}81
#endregion82

83

添加,删除网站的方法#region 添加,删除网站的方法84
public static void CreateNewWebSite(string hostIP, string portNum, string descOfWebSite, string commentOfWebSite, string webPath)85

{86
if (!EnsureNewSiteEnavaible(hostIP + portNum + descOfWebSite))87

{88
throw new ArgumentNullException("已经有了这样的网站了。" + Environment.NewLine + hostIP + portNum + descOfWebSite);89
}90

91
string entPath = String.Format("IIS://{0}/w3svc", HostName);92
DirectoryEntry rootEntry = GetDirectoryEntry(entPath);//取得iis路径93
string newSiteNum = GetNewWebSiteID(); //取得新网站ID94
DirectoryEntry newSiteEntry = rootEntry.Children.Add(newSiteNum, "IIsWebServer"); //增加站点95
newSiteEntry.CommitChanges();//保存对区域的更改(这里对站点的更改)96
newSiteEntry.Properties["ServerBindings"].Value = hostIP + portNum + descOfWebSite;97
newSiteEntry.Properties["ServerComment"].Value = commentOfWebSite;98
newSiteEntry.CommitChanges();99
DirectoryEntry vdEntry = newSiteEntry.Children.Add("root", "IIsWebVirtualDir");100
vdEntry.CommitChanges();101
vdEntry.Properties["Path"].Value = webPath;102
vdEntry.CommitChanges();103

104
}105

/**//// <summary>106
/// 删除一个网站。根据网站名称删除。107
/// </summary>108
/// <param name="siteName">网站名称</param>109
public static void DeleteWebSiteByName(string siteName)110

{111
string siteNum = GetWebSiteNum(siteName);112
string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);113
DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);114
string rootPath = String.Format("IIS://{0}/w3svc", HostName);115
DirectoryEntry rootEntry = GetDirectoryEntry(rootPath);116
rootEntry.Children.Remove(siteEntry);117
rootEntry.CommitChanges();118
}119
#endregion120

121

Start和Stop网站的方法#region Start和Stop网站的方法122
public static void StartWebSite(string siteName)123

{124
string siteNum = GetWebSiteNum(siteName);125
string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);126
DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);127

siteEntry.Invoke("Start", new object[]
{ });128
}129
public static void StopWebSite(string siteName)130

{131
string siteNum = GetWebSiteNum(siteName);132
string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);133
DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);134

siteEntry.Invoke("Stop", new object[]
{ });135
}136
#endregion137

138

确认网站是否相同#region 确认网站是否相同139

/**//// <summary>140
/// 确定一个新的网站与现有的网站没有相同的。141
/// 这样防止将非法的数据存放到IIS里面去142
/// </summary>143
/// <param name="bindStr">网站邦定信息</param>144
/// <returns>真为可以创建,假为不可以创建</returns>145
public static bool EnsureNewSiteEnavaible(string bindStr)146

{147
string entPath = String.Format("IIS://{0}/w3svc", HostName);148
DirectoryEntry ent = GetDirectoryEntry(entPath);149
foreach (DirectoryEntry child in ent.Children)150

{151
if (child.SchemaClassName == "IIsWebServer")152

{153
if (child.Properties["ServerBindings"].Value != null)154

{155
if (child.Properties["ServerBindings"].Value.ToString() == bindStr)156

{157
return false;158
}159
}160
}161
}162
return true;163
}164

165
#endregion166

167

获取一个网站编号//一个输入参数为站点描述#region 获取一个网站编号//一个输入参数为站点描述168

/**//// <summary>169
/// 输入参数为 站点的描述名 默认是站点描述为 "默认网站"170
/// </summary>171
/// <param name="siteName">站点描述</param>172
/// <exception cref="NotFoundWebSiteException">表示没有找到网站</exception>173
public static string GetWebSiteNum(string siteName)174

{175
Regex regex = new Regex(siteName);176
string tmpStr;177
string entPath = String.Format("IIS://{0}/w3svc", HostName);178
DirectoryEntry ent = GetDirectoryEntry(entPath);179
foreach (DirectoryEntry child in ent.Children)180

{181
if (child.SchemaClassName == "IIsWebServer")182

{183
if (child.Properties["ServerBindings"].Value != null)184

{185
tmpStr = child.Properties["ServerBindings"].Value.ToString();186
if (regex.Match(tmpStr).Success)187

{188
return child.Name;189
}190
}191
if (child.Properties["ServerComment"].Value != null)192

{193
tmpStr = child.Properties["ServerComment"].Value.ToString();194
if (regex.Match(tmpStr).Success)195

{196
return child.Name;197
}198
}199
}200
}201
throw new Exception("没有找到我们想要的站点" + siteName);202
}203
#endregion204

205

获取新网站id的方法#region 获取新网站id的方法206

/**//// <summary>207
/// 获取网站系统里面可以使用的最小的ID。208
/// 这是因为每个网站都需要有一个唯一的编号,而且这个编号越小越好。209
/// 这里面的算法经过了测试是没有问题的。210
/// </summary>211
/// <returns>最小的id</returns>212
public static string GetNewWebSiteID()213

{214
ArrayList list = new ArrayList();215
string tmpStr;216
string entPath = String.Format("IIS://{0}/w3svc", HostName);217
DirectoryEntry ent = GetDirectoryEntry(entPath);218
foreach (DirectoryEntry child in ent.Children)219

{220
if (child.SchemaClassName == "IIsWebServer")221

{222
tmpStr = child.Name.ToString();223
list.Add(Convert.ToInt32(tmpStr));224
}225
}226
list.Sort();227
int i = 1;228
foreach (int j in list)229

{230
if (i == j)231

{232
i++;233
}234
}235
return i.ToString();236
}237
#endregion238

239

增加,删除主机头#region 增加,删除主机头240

/**//// <summary>241
/// 增加主机头242
/// </summary>243
/// <param name="HostName">站点描述</param>244
/// <param name="ip">ip</param>245
/// <param name="port">端口</param>246
/// <param name="domain">域名</param>247
public static void AddHostHeader(string HostName, string ip, int port, string domain)//增加主机头(站点编号.ip.端口.域名)248

{249
DirectoryEntry site = new DirectoryEntry(String.Format("IIS://{0}/w3svc", HostName));250
PropertyValueCollection serverBindings = site.Properties["ServerBindings"];251
string headerStr = string.Format("{0}:{1}:{2}", ip, port, domain);252
if (!serverBindings.Contains(headerStr))253

{254
serverBindings.Add(headerStr);255
}256
site.CommitChanges();257
}258

/**//// <summary>259
/// 删除主机头260
/// </summary>261
/// <param name="HostName">站点描述</param>262
/// <param name="ip">ip</param>263
/// <param name="port">端口</param>264
/// <param name="domain">域名</param>265
public static void DeleteHostHeader(string HostName, string ip, int port, string domain)//删除主机头(站点编号.ip.端口.域名)266

{267
DirectoryEntry site = new DirectoryEntry(String.Format("IIS://{0}/w3svc", HostName));268
PropertyValueCollection serverBindings = site.Properties["ServerBindings"];269
string headerStr = string.Format("{0}:{1}:{2}", ip, port, domain);270
if (serverBindings.Contains(headerStr))271

{272
serverBindings.Remove(headerStr);273
}274
site.CommitChanges();275
}276
#endregion277

278
}279
}280

有生命力的东西不需要张扬...
浙公网安备 33010602011771号