可以新建,删除网站,虚拟目录等
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
using System;
6
using System.Data;
7
using System.DirectoryServices;
8
using System.Collections;
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
namespace IISManage
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////<summary>
13
/// IIS管理类,可以实现创建站点、虚拟目录,删除虚拟目录等 ///
14
/// </summary>
15
public class IISManager
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
private string _server, _website, _AnonymousUserPass, _AnonymousUserName;
19
private VirtualDirectories _virdirs;
20
protected System.DirectoryServices.DirectoryEntry rootfolder;
21
private bool _batchflag;
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
构造函数#region 构造函数
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
26
/// 构造函数 ///
27
/// </summary>
28
public IISManager()
29![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
30
//默认情况下使用localhost,即访问本地机
31
_server = "localhost"; _website = "1"; _batchflag = false;
32
}
33![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
36
/// 构造函数
37
/// </summary>
38
/// <param name="strServer">服务器</param>
39
public IISManager(string strServer)
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
41
_server = strServer;
42
_website = "1"; _batchflag = false;
43
}
44![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
47
/// 构造函数
48
/// </summary>
49
/// <param name="strServer">服务器</param>
50
/// <param name="website">站点,每一个站点为1,第二个站点为2,依此类推</param>
51
public IISManager(string strServer, int website)
52![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53
_server = strServer;
54
_website = website.ToString();
55
_batchflag = false;
56
}
57
#endregion #region 定义公共属性
58![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
61
/// 匿名访问用户
62
/// </summary>
63
public string AnonymousUserName
64![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _AnonymousUserName; }
66![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _AnonymousUserName = value; }
67
}
68![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
71
/// 匿名访问用户密码
72
/// </summary>
73
public string AnonymousUserPass
74![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _AnonymousUserPass; }
76![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _AnonymousUserPass = value; }
77
}
78![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
81
/// 服务器,可以是IP或计算名
82
/// </summary>
83
public string Server
84![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
85![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _server; }
86![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _server = value; }
87
}
88![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
90
/// 站点,一般来说第一台主机为1,第二台主机为2,依次类推
91
/// </summary>
92
public int WebSite
93![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
94![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return Convert.ToInt32(_website); }
95![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _website = Convert.ToString(value); }
96
}
97![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
99![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
100
/// 虚拟目录的名字
101
/// </summary>
102
public VirtualDirectories VirDirs
103![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
104![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _virdirs; }
105![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _virdirs = value; }
106
}
107![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
109![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
110![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
定义公共方法#region 定义公共方法
111![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
112
/// 获取匿名访问用户的用户名和密码
113
/// </summary>
114
public void get_AnonymousUser()
115![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
116
_AnonymousUserPass = "IUSR_DEVE-SERVER";
117
_AnonymousUserName = "IUSR_DEVE-SERVER";
118
VirtualDirectory vDir;
119
try
120![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
121
Hashtable myList = (Hashtable)_virdirs;
122
IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
123
while (myEnumerator.MoveNext())
124![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
125
vDir = (VirtualDirectory)myEnumerator.Value;
126
if (vDir.AnonymousUserName != "" && vDir.AnonymousUserPass != "")
127![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
128
_AnonymousUserName = vDir.AnonymousUserName;
129
_AnonymousUserPass = vDir.AnonymousUserPass;
130
break;
131
}
132
}
133
}
134
catch
135![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
136
_AnonymousUserPass = "IUSR_DEVE-SERVER";
137
_AnonymousUserName = "IUSR_DEVE-SERVER";
138
}
139
}
140![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
141![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
142![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
143
/// 连接服务器
144
/// </summary>
145
public void Connect()
146![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
147
ConnectToServer();
148
}
149![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
150
/// 连接服务器
151
/// </summary>
152
/// <param name="strServer">服务器名称</param>
153
public void Connect(string strServer)
154![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
155
_server = strServer;
156
ConnectToServer();
157
}
158![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
159
/// 连接服务器
160
/// </summary>
161
/// <param name="strServer">服务器名称</param>
162
/// <param name="strWebSite">站点,一般来说第一台主机为1,第二台主机为2,依次类推</param>
163
public void Connect(string strServer, string strWebSite)
164![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
165
_server = strServer;
166
_website = strWebSite;
167
ConnectToServer();
168
}
169![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
170![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
171![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
172
/// 添加一个站点
173
/// </summary>
174
/// <param name="path">站点的路径</param>
175
public void CreateWebSite(string webSiteName, string port, string path)
176![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
177
try
178![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
179
DirectoryEntry root = new DirectoryEntry("IIS://" + this._server + "/W3SVC");
180
int siteID = 1;
181
foreach (DirectoryEntry e in root.Children)
182![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
183
if (e.SchemaClassName == "IIsWebServer")
184![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
185
int ID = Convert.ToInt32(e.Name);
186![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (ID >= siteID)
{ siteID = ID + 1; }
187
if (e.Properties["ServerBindings"][0].ToString() == ":" + port + ":")
188![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
189
throw new Exception(port + "已被占用,请选择其它端口!");
190
}
191
}
192
}
193
DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", siteID);
194
site.Invoke("Put", "ServerComment", webSiteName); site.Invoke("Put", "KeyType", "IIsWebServer");
195
site.Invoke("Put", "ServerBindings", ":" + port + ":"); site.Invoke("Put", "ServerState", 2);
196
site.Invoke("Put", "FrontPageWeb", 1); site.Invoke("Put", "DefaultDoc", "index.aspx");
197
site.Invoke("Put", "SecureBindings", ":443:"); site.Invoke("Put", "ServerAutoStart", 1);
198
site.Invoke("Put", "ServerSize", 1); site.Invoke("SetInfo");
199
DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir");
200
siteVDir.Properties["AppIsolated"][0] = 2;
201
siteVDir.Properties["Path"][0] = path;
202
siteVDir.Properties["AccessFlags"][0] = 513;
203
siteVDir.Properties["FrontPageWeb"][0] = 1;
204
siteVDir.Properties["AppRoot"][0] = "LM/W3SVC/" + siteID + "/Root";
205
siteVDir.Properties["AppFriendlyName"][0] = "ROOT";
206
siteVDir.CommitChanges(); site.CommitChanges();
207
}
208
catch (Exception ex)
209![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
210
throw ex;
211
}
212
}
213![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
214![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
215![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
216
/// 取得所有站点
217
/// </summary>
218
/// <returns></returns>
219
public System.Collections.Hashtable getSites()
220![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
221
try
222![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
223
DirectoryEntry root = new DirectoryEntry("IIS://" + this._server + "/W3SVC");
224
Hashtable sites = new Hashtable();
225
foreach (DirectoryEntry e in root.Children)
226![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
227
if (e.SchemaClassName == "IIsWebServer")
228![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
229
sites.Add(e.Name, e);
230
}
231
}
232
return sites;
233
}
234
catch (Exception ex)
235![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
236
throw ex;
237
}
238
}
239![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
240![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
241![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
242
/// 判断是否存这个虚拟目录
243
/// </summary>
244
/// <param name="strVirdir">虚拟目录名称</param>
245
/// <returns></returns>
246
public bool Exists(string strVirdir)
247![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
248
return _virdirs.Contains(strVirdir);
249
}
250![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
251
/// 添加一个虚拟目录
252
/// </summary>
253
/// <param name="newdir">虚拟目录</param>
254
/// <returns></returns>
255
public bool CreateVirtualDirectory(VirtualDirectory newdir)
256![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
257
string strPath = "IIS://" + _server + "/W3SVC/" + _website + "/ROOT/" + newdir.Name;
258
if (!_virdirs.Contains(newdir.Name) || _batchflag)
259![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
260
try
261![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
262
//加入到ROOT的Children集合中去
263
DirectoryEntry newVirDir = rootfolder.Children.Add(newdir.Name, "IIsWebVirtualDir");
264
newVirDir.Invoke("AppCreate", true);
265
newVirDir.CommitChanges();
266
rootfolder.CommitChanges();
267
//然后更新数据
268
UpdateDirInfo(newVirDir, newdir);
269
return true;
270
}
271
catch (Exception)
272![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
273
return false;
274
}
275
}
276
else
277![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
278
return false;
279
}
280
}
281![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
282
/// 得到一个虚拟目录
283
/// </summary>
284
/// <param name="strVirdir">虚拟目录名</param>
285
/// <returns></returns>
286
public VirtualDirectory GetVirDir(string strVirdir)
287![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
288
VirtualDirectory tmp = null;
289
if (_virdirs.Contains(strVirdir))
290![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
291
tmp = _virdirs.Find(strVirdir);
292
((VirtualDirectory)_virdirs[strVirdir]).flag = 2;
293
}
294
else
295![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
296
throw new Exception("虚拟目录" + strVirdir + "不存在!");
297
}
298
return tmp;
299
}
300![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
301![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
302![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
303
/// 更新一个虚拟目录
304
/// </summary>
305
/// <param name="dir">虚拟目录</param>
306
public void Update(VirtualDirectory dir)
307![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
308
//判断需要更改的虚拟目录是否存在
309
if (_virdirs.Contains(dir.Name))
310![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
311
DirectoryEntry ode = rootfolder.Children.Find(dir.Name, "IIsWebVirtualDir");
312
UpdateDirInfo(ode, dir);
313
}
314
else
315![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
316
System.Windows.Forms.MessageBox.Show("虚拟目录" + dir.Name + "不存在!");
317
}
318
}
319![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
320
/// 删除一个虚拟目录
321
/// </summary>
322
/// <param name="strVirdir">虚拟目录名</param>
323
public void Delete(string strVirdir)
324![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
325
if (_virdirs.Contains(strVirdir))
326![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
327
object[] paras = new object[2];
328
paras[0] = "IIsWebVirtualDir";
329
//表示操作的是虚拟目录
330
paras[1] = strVirdir;
331
rootfolder.Invoke("Delete", paras);
332
rootfolder.CommitChanges();
333
}
334
else
335![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
336
System.Windows.Forms.MessageBox.Show("虚拟目录" + strVirdir + "不存在!");
337
}
338
}
339![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
340
/// 批量更新
341
/// </summary>
342
public void UpdateBatch()
343![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
344
BatchUpdate(_virdirs);
345
}
346![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
347![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
348![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
349
/// 批量更新
350
/// </summary>
351
/// <param name="vds">虚拟目录集合</param>
352
public void UpdateBatch(VirtualDirectories vds)
353![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
354
BatchUpdate(vds);
355
}
356![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
357![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
358![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
359
/// 获取虚拟目录集合
360
/// </summary>
361
/// <returns></returns>
362
public VirtualDirectories GetVirtualDirdctories()
363![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
364
VirtualDirectories vds = GetVirDirs(this.rootfolder.Children);
365
return vds;
366
}
367
#endregion
368![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
369![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
私有方法#region 私有方法
370![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
371![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
372![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
373
/// 关闭当前对象
374
/// </summary>
375
public void Close()
376![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
377
_virdirs.Clear();
378
_virdirs = null;
379
rootfolder.Dispose();
380
}
381![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
382
/// 连接服务器
383
/// </summary>
384
private void ConnectToServer()
385![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
386
string strPath = "IIS://" + _server + "/W3SVC/" + _website + "/ROOT";
387
try
388![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
389
this.rootfolder = new DirectoryEntry(strPath);
390
_virdirs = GetVirDirs(this.rootfolder.Children);
391
}
392
catch (Exception)
393![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
394
System.Windows.Forms.MessageBox.Show("无法连接到服务器:" + _server);
395
}
396
}
397![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
398
/// 执行批量更新
399
/// </summary>
400
/// <param name="vds">虚拟目录集合</param>
401
private void BatchUpdate(VirtualDirectories vds)
402![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
403
_batchflag = true;
404
foreach (object item in vds.Values)
405![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
406
VirtualDirectory vd = (VirtualDirectory)item;
407
switch (vd.flag)
408![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
409
case 0:
410
break;
411
case 1:
412
CreateVirtualDirectory(vd);
413
break;
414
case 2:
415
Update(vd);
416
break;
417
}
418
}
419
_batchflag = false;
420
}
421![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
422
/// 更新指定虚拟目录
423
/// </summary>
424
/// <param name="de"></param>
425
/// <param name="vd">要执行更新的虚拟目录</param>
426
private void UpdateDirInfo(DirectoryEntry de, VirtualDirectory vd)
427![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
428
de.Properties["AnonymousUserName"][0] = vd.AnonymousUserName;
429
de.Properties["AnonymousUserPass"][0] = vd.AnonymousUserPass;
430
de.Properties["AccessRead"][0] = vd.AccessRead;
431
de.Properties["AccessExecute"][0] = vd.AccessExecute;
432
de.Properties["AccessWrite"][0] = vd.AccessWrite;
433
de.Properties["AuthBasic"][0] = vd.AuthBasic;
434
de.Properties["AuthNTLM"][0] = vd.AuthNTLM;
435
de.Properties["ContentIndexed"][0] = vd.ContentIndexed;
436
de.Properties["EnableDefaultDoc"][0] = vd.EnableDefaultDoc;
437
de.Properties["EnableDirBrowsing"][0] = vd.EnableDirBrowsing;
438
de.Properties["AccessSSL"][0] = vd.AccessSSL;
439
de.Properties["AccessScript"][0] = vd.AccessScript;
440
de.Properties["DefaultDoc"][0] = vd.DefaultDoc;
441
de.Properties["Path"][0] = vd.Path;
442
de.CommitChanges();
443
}
444![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
445![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
446![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// 获取虚拟目录集合
447
/// </summary>
448
/// <param name="des"></param>
449
/// <returns></returns>
450
private VirtualDirectories GetVirDirs(DirectoryEntries des)
451![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
452
VirtualDirectories tmpdirs = new VirtualDirectories();
453
foreach (DirectoryEntry de in des)
454![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
455
if (de.SchemaClassName == "IIsWebVirtualDir")
456![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
457
VirtualDirectory vd = new VirtualDirectory();
458
vd.Name = de.Name;
459
vd.AccessRead = (bool)de.Properties["AccessRead"][0];
460
vd.AccessExecute = (bool)de.Properties["AccessExecute"][0];
461
vd.AccessWrite = (bool)de.Properties["AccessWrite"][0];
462
vd.AnonymousUserName = (string)de.Properties["AnonymousUserName"][0];
463
vd.AnonymousUserPass = (string)de.Properties["AnonymousUserName"][0];
464
vd.AuthBasic = (bool)de.Properties["AuthBasic"][0];
465
vd.AuthNTLM = (bool)de.Properties["AuthNTLM"][0];
466
vd.ContentIndexed = (bool)de.Properties["ContentIndexed"][0];
467
vd.EnableDefaultDoc = (bool)de.Properties["EnableDefaultDoc"][0];
468
vd.EnableDirBrowsing = (bool)de.Properties["EnableDirBrowsing"][0];
469
vd.AccessSSL = (bool)de.Properties["AccessSSL"][0];
470
vd.AccessScript = (bool)de.Properties["AccessScript"][0];
471
vd.Path = (string)de.Properties["Path"][0];
472
vd.flag = 0; vd.DefaultDoc = (string)de.Properties["DefaultDoc"][0];
473
tmpdirs.Add(vd.Name, vd);
474
}
475
}
476
return tmpdirs;
477
}
478
#endregion
479
}
480![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
481
/// 虚拟目录实体
482
/// </summary>
483
public class VirtualDirectory
484![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
485
private bool _read, _execute, _script, _ssl, _write, _authbasic, _authntlm, _indexed, _endirbrow, _endefaultdoc;
486
private string _ausername, _auserpass, _name, _path;
487
private int _flag;
488
private string _defaultdoc;
489![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
490![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
491
/// 构造函数
492
/// </summary>
493
public VirtualDirectory()
494![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
495
SetValue();
496
}
497![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
498![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
499![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
500
/// 构造函数
501
/// </summary>
502
/// <param name="sVirDirName">虚拟目录名</param>
503
public VirtualDirectory(string sVirDirName)
504![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
505
SetValue();
506
_name = sVirDirName;
507
}
508![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
509
/// 构造函数
510
/// </summary>
511
/// <param name="sVirDirName">虚拟目录名</param>
512
/// <param name="strPhyPath">物理路径</param>
513
/// <param name="AnonymousUser"></param>
514
public VirtualDirectory(string sVirDirName, string strPhyPath, string[] AnonymousUser)
515![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
516
SetValue();
517
_name = sVirDirName;
518
_path = strPhyPath;
519
_ausername = AnonymousUser[0];
520
_auserpass = AnonymousUser[1];
521
}
522![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
523![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
524![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
525![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
526
/// 设置虚拟目录的属性
527
/// </summary>
528
private void SetValue()
529![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
530
_read = true;
531
_execute = false;
532
_script = true;
533
_ssl = false;
534
_write = false;
535
_authbasic = false;
536
_authntlm = true;
537
_indexed = true;
538
_endirbrow = false;
539
_endefaultdoc = true;
540
_flag = 1;
541
_defaultdoc = "index.aspx";
542
_path = "C:\\";
543
_ausername = "IUSR_DEVE-SERVER";
544
_auserpass = "IUSR_DEVE-SERVER";
545
_name = "";
546
}
547![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
548![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
549
public override string ToString()
550![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
551
return this._name;
552
}
553![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
554![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
定义属性#region 定义属性
555
public int flag
556![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
557![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _flag; }
558![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _flag = value; }
559
}
560
public bool AccessRead
561![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
562![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _read; }
563![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _read = value; }
564
}
565
public bool AccessWrite
566![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
567![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _write; }
568![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _write = value; }
569
}
570
public bool AccessExecute
571![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
572![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _execute; }
573![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _execute = value; }
574
}
575
public bool AccessSSL
576![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
577![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _ssl; }
578![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _ssl = value; }
579
}
580
public bool AccessScript
581![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
582![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _script; }
583![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _script = value; }
584
}
585
public bool AuthBasic
586![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
587![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _authbasic; }
588![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _authbasic = value; }
589
}
590
public bool AuthNTLM
591![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
592![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _authntlm; }
593![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _authntlm = value; }
594
}
595
public bool ContentIndexed
596![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
597![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _indexed; }
598![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _indexed = value; }
599
}
600
public bool EnableDirBrowsing
601![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
602![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _endirbrow; }
603![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _endirbrow = value; }
604
}
605
public bool EnableDefaultDoc
606![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
607![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _endefaultdoc; }
608![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _endefaultdoc = value; }
609
}
610
public string Name
611![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
612![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _name; }
613![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _name = value; }
614
}
615
public string Path
616![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
617![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _path; }
618![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _path = value; }
619
}
620
public string DefaultDoc
621![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
622![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _defaultdoc; }
623![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _defaultdoc = value; }
624
}
625
public string AnonymousUserName
626![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
627![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _ausername; }
628![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _ausername = value; }
629
}
630
public string AnonymousUserPass
631![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
632![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _auserpass; }
633![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _auserpass = value; }
634
}
635
#endregion
636
}
637![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
638![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
639![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
640
/// 虚拟目录集合类
641
/// </summary>
642
public class VirtualDirectories : System.Collections.Hashtable
643![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
644![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public VirtualDirectories()
{ }
645![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
646
/// 搜索指定的虚拟目录
647
/// </summary>
648
/// <param name="strName">虚拟目录名</param>
649
/// <returns></returns>
650
public VirtualDirectory Find(string strName)
651![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
652
return (VirtualDirectory)this[strName];
653
}
654
}
655
}
656![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
posted @
2007-07-06 00:49
吴碧宇
阅读(
978)
评论()
编辑
收藏
举报