System.DirectoryServices命名空间中包括了些强大的东东--DirectoryEntry,DirectoryEntries,它们为我们提供了访问活动目录的强大功能,在这些类允许我们操作IIS、LDAP、NDS以及WinNT,功能很强大的吧:)
不过我们此处只谈IIS的控制,一般来说,我们操作IIS一般都是对虚拟目录的操作,因此我将此列为主要的内容来讲。
首先我们要搞清楚IIS的层次结构的问题,下面是我从国外找来的一张图,很好的解释了IIS的层次结构:
[htmChina:Image id=Image1|12][/htmChina:Image]为了搞清楚IIS的控制语法,我们就必须搞清上图,了解IIS元数据(Metabase)的层次结构。图中的每一个节点称之Key,而每个Key可以包含一个或多个值,这些值就是我们说的属性(properties),IIS元数据中的Key与IIS中的元素是相符的,因此元数据中的属性值的设定是会影响IIS中的设置。这就是我们编程的基本思路和核心。
另外还要了解一下Schema这个概念。它表示IIS中构架的名称,即可以理解IIS元数据中Key的类型,具体点说就是指每个结点的类型。我们知道,IIS中有虚拟目录,普通目录,以及文件这些东东,而这些都属于IIS的元素,区分的他们的标帜就是Schema。比如虚拟目录的Schema就是"IIsVirtualDir",普通目录就是"IIsWebDir"。这样我们添加、删除目录时,IIS就知道我们添加的是虚拟目录还是普通目录。
创建虚拟目录
DirectoryEntry是.Net给我们的一大礼物,他的名字我们就知道他的功能--目录入口。使用过ADSI的人都知道操作IIS,WinNT这些时,我们还需要提供他们的Path,操作IIS时,这个Path的格式为:
IIS://ComputerName/Service/Website/Directory
- ComputerName:即操作的服务器的名字,可以是名字也可以是IP,经常用的就是localhost
- Service:即操作的服务器,IIS中有Web,也有FTP,还有SMTP这些服务,我们主要是操作IIS的Web功能,因此此处就是"W3SVC",如果是FTP则应是"MSFTPSVC"
- WebSite:一个IIS服务中可以包括很多的站点,这个就用于设置操作的站点。他的值是一个数字,默认是1,表示缺省站点,如果有其它,则从1开始依次类推。
- Directory:不用说,即操作的目录名称,一个站点一般顶层目录为"ROOT",其它目录则是他的孩子(Child)。
首先我们获取一个站点的顶层目录(根目录):
如果我们创建这个对象是没有发生异常,则表示这个目录是真实存在的。
下面我们来添加新的虚拟目录,比如我们要加的是"Aspcn":
newVirDir.Invoke("AppCreate",true);
newVirDir.CommitChanges();
rootfolder.CommitChanges();
创建目录的思路很简单,即在根目录的子集(rootfolder.Children)中再添加一条记录,这里使用的是DirectoryEntries类中的Add方法,它返回的是一个DirectoryEntry,表示新加入的目录,第一个参数是虚拟目录的名字,第二个则是Schema的类名以表明我们加入的目录类型。然后再使用DirectoryEntry的Invoke方法,调用ADSI中的"AppCreate"方法将目录真正创建(似乎不走这一步也可以创建目录成功,但是为了保险起见,大家还是用吧),最后便是依次调用新、根目录的CommitChanges方法,确认此次操作。
在创建新目录时,我们也可以同时给这个目录的属性赋值,但是我的实战经验告诉我,最好不要这样做,如果创建时就赋值,将有很多属性不能赋值成功,比如重要的表示真实目录的Path属性。因此飞刀建议大家最好是先创建目录,然后再赋值,即更新目录信息。
更新虚拟目录
相信大家对IIS都比较熟悉,了解IIS中一些重要的设置,如可读(AccessRead)、可写(AccessWrite)、可执行(AccessExecute)等。这些都可通过对DirectoryEntry的Properties属性集合的赋值来实现。赋值可以通过两种方式来完成:
第一种是调用Properties集合的Add方法,如:
第二种是对第一个索引值赋值:
这两种方法都是可行的。具体是要看你的喜好了。
在进行赋值之前我们还是要确定要要赋值的目标吧:)这里我们使用DirectoryEntries类的Find方法,如:
找到了,我们就可以赋值了。赋值时一定要好好看看啊,虚拟目录的属性值可以超多,一查一大堆。。:(太多了,飞刀我也不重复了,大家去微软的站点上查:)
比较常用的有:AccessRead,AccessWrite,AccessExecute,AccessScript,DefaultDoc,EnableDefaultDoc,Path
删除虚拟目录
删除虚拟目录的方法也很简单,就是找到你要删除的虚拟目录,然后调用AppDelete方法。
de.Invoke("AppDelete",true);
rootfolder.CommitChanges();
还有一种方法,就是调用Root目录的Delete方法。
paras[0] = "IIsWebVirtualDir"; //表示操作的是虚拟目录
paras[1] = "Aspcn";
rootfolder.Invoke("Delete",paras);
rootfolder.CommitChanges();
喜欢哪一种就看编程习惯了:))
using System;2
using System.Data;3
using System.DirectoryServices;4
using System.Collections;5
namespace Aspcn.Management6
{7
/// <summary>8
/// IISManager 的摘要说明。9
/// </summary> 10
public class IISManager 11
{ 12
//定义需要使用的 13
private string _server,_website; 14
private VirtualDirectories _virdirs; 15
protected System.DirectoryServices.DirectoryEntry rootfolder;16
private bool _batchflag; 17
public IISManager() 18
{ 19
//默认情况下使用localhost,即访问本地机 20
_server = "localhost"; 21
_website = "1"; 22
_batchflag = false; 23
} 24
public IISManager(string strServer) 25
{ 26
_server = strServer; 27
_website = "1"; 28
_batchflag = false; 29
} 30
/// <summary> 31
/// 定义公共属性 32
/// </summary> 33
34
//Server属性定义访问机器的名字,可以是IP与计算名 35
public string Server 36
{ 37
get{ return _server;} 38
set{ _server = value;} 39
} 40
//WebSite属性定义,为一数字,为方便,使用string 41
//一般来说第一台主机为1,第二台主机为2,依次类推 42
public string WebSite 43
{ 44
get{ return _website; } 45
set{ _website = value; } 46
} 47
48
//虚拟目录的名字 49
public VirtualDirectories VirDirs 50
{ 51
get{ return _virdirs; } 52
set{ _virdirs = value;} 53
} 54
///<summary> 55
///定义公共方法 56
///</summary> 57
58
//连接服务器 59
public void Connect() 60
{ 61
ConnectToServer(); 62
} 63
//为方便重载 64
public void Connect(string strServer) 65
{ 66
_server = strServer; 67
ConnectToServer(); 68
} 69
//为方便重载 70
public void Connect(string strServer,string strWebSite) 71
{ 72
_server = strServer; 73
_website = strWebSite; 74
ConnectToServer(); 75
} 76
//判断是否存这个虚拟目录 77
public bool Exists(string strVirdir) 78
{ 79
return _virdirs.Contains(strVirdir); 80
} 81
//添加一个虚拟目录 82
public void Create(VirtualDirectory newdir) 83
{ 84
string strPath = "IIS://" + _server + "/W3SVC/" + _website + "/ROOT/" + newdir.Name; 85
if(!_virdirs.Contains(newdir.Name) || _batchflag ) 86
{ 87
try 88
{ 89
//加入到ROOT的Children集合中去 90
DirectoryEntry newVirDir = rootfolder.Children.Add(newdir.Name,"IIsWebVirtualDir"); 91
newVirDir.Invoke("AppCreate",true); 92
newVirDir.CommitChanges(); 93
rootfolder.CommitChanges(); 94
//然后更新数据 95
UpdateDirInfo(newVirDir,newdir); 96
} 97
catch(Exception ee) 98
{ 99
throw new Exception(ee.ToString()); 100
} 101
} 102
else 103
{ 104
throw new Exception("This virtual directory is already exist."; 105
} 106
} 107
//得到一个虚拟目录 108
public VirtualDirectory GetVirDir(string strVirdir) 109
{ 110
VirtualDirectory tmp = null; 111
if(_virdirs.Contains(strVirdir)) 112
{ 113
tmp = _virdirs.Find(strVirdir); 114
((VirtualDirectory)_virdirs[strVirdir]).flag = 2; 115
} 116
else 117
{ 118
throw new Exception("This virtual directory is not exists"; 119
} 120
return tmp; 121
} 122
123
//更新一个虚拟目录 124
public void Update(VirtualDirectory dir) 125
{ 126
//判断需要更改的虚拟目录是否存在 127
if(_virdirs.Contains(dir.Name)) 128
{ 129
DirectoryEntry ode = rootfolder.Children.Find(dir.Name,"IIsWebVirtualDir"); 130
UpdateDirInfo(ode,dir); 131
} 132
else 133
{ 134
throw new Exception("This virtual directory is not exists."; 135
} 136
} 137
138
//删除一个虚拟目录 139
public void Delete(string strVirdir) 140
{ 141
if(_virdirs.Contains(strVirdir)) 142
{ 143
object[] paras = new object[2]; 144
paras[0] = "IIsWebVirtualDir"; //表示操作的是虚拟目录 145
paras[1] = strVirdir; 146
rootfolder.Invoke("Delete",paras); 147
rootfolder.CommitChanges(); 148
} 149
else 150
{ 151
throw new Exception("Can't delete " + strVirdir + ",because it isn't exists."; 152
} 153
} 154
//批量更新 155
public void UpdateBatch() 156
{ 157
BatchUpdate(_virdirs); 158
} 159
//重载一个:-) 160
public void UpdateBatch(VirtualDirectories vds) 161
{ 162
BatchUpdate(vds); 163
} 164
165
///<summary> 166
///私有方法 167
///</summary> 168
169
//连接服务器 170
private void ConnectToServer() 171
{ 172
string strPath = "IIS://" + _server + "/W3SVC/" + _website +"/ROOT"; 173
try 174
{ 175
this.rootfolder = new DirectoryEntry(strPath); 176
_virdirs = GetVirDirs(this.rootfolder.Children); 177
} 178
catch(Exception e) 179
{ 180
throw new Exception("Can't connect to the server ["+ _server +"]
",e); 181
} 182
} 183
//执行批量更新 184
private void BatchUpdate(VirtualDirectories vds) 185
{ 186
_batchflag = true; 187
foreach(object item in vds.Values) 188
{ 189
VirtualDirectory vd = (VirtualDirectory)item; 190
switch(vd.flag) 191
{ 192
case 0: 193
break; 194
case 1: 195
Create(vd); 196
break; 197
case 2: 198
Update(vd); 199
break; 200
} 201
} 202
_batchflag = false; 203
} 204
//更新东东 205
private void UpdateDirInfo(DirectoryEntry de,VirtualDirectory vd) 206
{ 207
de.Properties["AnonymousUserName"][0] = vd.AnonymousUserName; 208
de.Properties["AnonymousUserPass"][0] = vd.AnonymousUserPass; 209
de.Properties["AccessRead"][0] = vd.AccessRead; 210
de.Properties["AccessExecute"][0] = vd.AccessExecute; 211
de.Properties["AccessWrite"][0] = vd.AccessWrite; 212
de.Properties["AuthBasic"][0] = vd.AuthBasic; 213
de.Properties["AuthNTLM"][0] = vd.AuthNTLM; 214
de.Properties["ContentIndexed"][0] = vd.ContentIndexed; 215
de.Properties["EnableDefaultDoc"][0] = vd.EnableDefaultDoc; 216
de.Properties["EnableDirBrowsing"][0] = vd.EnableDirBrowsing; 217
de.Properties["AccessSSL"][0] = vd.AccessSSL; 218
de.Properties["AccessScript"][0] = vd.AccessScript; 219
de.Properties["DefaultDoc"][0] = vd.DefaultDoc; 220
de.Properties["Path"][0] = vd.Path; 221
de.CommitChanges(); 222
} 223
224
//获取虚拟目录集合 225
private VirtualDirectories GetVirDirs(DirectoryEntries des) 226
{ 227
VirtualDirectories tmpdirs = new VirtualDirectories(); 228
foreach(DirectoryEntry de in des) 229
{ 230
if(de.SchemaClassName == "IIsWebVirtualDir" 231
{ 232
VirtualDirectory vd = new VirtualDirectory(); 233
vd.Name = de.Name; 234
vd.AccessRead = (bool)de.Properties["AccessRead"][0]; 235
vd.AccessExecute = (bool)de.Properties["AccessExecute"][0]; 236
vd.AccessWrite = (bool)de.Properties["AccessWrite"][0]; 237
vd.AnonymousUserName = (string)de.Properties["AnonymousUserName"][0]; 238
vd.AnonymousUserPass = (string)de.Properties["AnonymousUserName"][0]; 239
vd.AuthBasic = (bool)de.Properties["AuthBasic"][0]; 240
vd.AuthNTLM = (bool)de.Properties["AuthNTLM"][0]; 241
vd.ContentIndexed = (bool)de.Properties["ContentIndexed"][0]; 242
vd.EnableDefaultDoc = (bool)de.Properties["EnableDefaultDoc"][0]; 243
vd.EnableDirBrowsing = (bool)de.Properties["EnableDirBrowsing"][0]; 244
vd.AccessSSL = (bool)de.Properties["AccessSSL"][0]; 245
vd.AccessScript = (bool)de.Properties["AccessScript"][0]; 246
vd.Path = (string)de.Properties["Path"][0]; 247
vd.flag = 0; 248
vd.DefaultDoc = (string)de.Properties["DefaultDoc"][0]; 249
tmpdirs.Add(vd.Name,vd); 250
} 251
} 252
return tmpdirs; 253
} 254
} 255
/// <summary> 256
/// VirtualDirectory类 257
/// </summary> 258
public class VirtualDirectory 259
{ 260
private bool _read,_execute,_script,_ssl,_write,_authbasic,_authntlm,_indexed,_endirbrow,_endefaultdoc; 261
private string _ausername,_auserpass,_name,_path; 262
private int _flag; 263
private string _defaultdoc; 264
/// <summary> 265
/// 构造函数 266
/// </summary> 267
public VirtualDirectory() 268
{ 269
SetValue(); 270
} 271
public VirtualDirectory(string strVirDirName) 272
{ 273
_name = strVirDirName; 274
SetValue(); 275
} 276
private void SetValue() 277
{ 278
_read = true;_execute = false;_script = false;_ssl= false;_write=false;_authbasic=false;_authntlm=false; 279
_indexed = false;_endirbrow=false;_endefaultdoc = false; 280
_flag = 1; 281
_defaultdoc = "default.htm,default.aspx,default.asp,index.htm"; 282
_path = "C:\\"; 283
_ausername = "";_auserpass ="";_name=""; 284
} 285
///<summary> 286
///定义属性,IISVirtualDir太多属性了 287
///我只搞了比较重要的一些,其它的大伙需要的自个加吧。 288
///</summary> 289
290
public int flag 291
{ 292
get{ return _flag;} 293
set{ _flag = value;} 294
} 295
public bool AccessRead 296
{ 297
get{ return _read;} 298
set{ _read = value;} 299
} 300
public bool AccessWrite 301
{ 302
get{ return _write;} 303
set{ _write = value;} 304
} 305
public bool AccessExecute 306
{ 307
get{ return _execute;} 308
set{ _execute = value;} 309
} 310
public bool AccessSSL 311
{ 312
get{ return _ssl;} 313
set{ _ssl = value;} 314
} 315
public bool AccessScript 316
{ 317
get{ return _script;} 318
set{ _script = value;} 319
} 320
public bool AuthBasic 321
{ 322
get{ return _authbasic;} 323
set{ _authbasic = value;} 324
} 325
public bool AuthNTLM 326
{ 327
get{ return _authntlm;} 328
set{ _authntlm = value;} 329
} 330
public bool ContentIndexed 331
{ 332
get{ return _indexed;} 333
set{ _indexed = value;} 334
} 335
public bool EnableDirBrowsing 336
{ 337
get{ return _endirbrow;} 338
set{ _endirbrow = value;} 339
} 340
public bool EnableDefaultDoc 341
{ 342
get{ return _endefaultdoc;} 343
set{ _endefaultdoc = value;} 344
} 345
public string Name 346
{ 347
get{ return _name;} 348
set{ _name = value;} 349
} 350
public string Path 351
{ 352
get{ return _path;} 353
set{ _path = value;} 354
} 355
public string DefaultDoc 356
{ 357
get{ return _defaultdoc;} 358
set{ _defaultdoc = value;} 359
} 360
public string AnonymousUserName 361
{ 362
get{ return _ausername;} 363
set{ _ausername = value;} 364
} 365
public string AnonymousUserPass 366
{ 367
get{ return _auserpass;} 368
set{ _auserpass = value;} 369
} 370
} 371
/// <summary> 372
/// 集合VirtualDirectories 373
/// </summary> 374
375
public class VirtualDirectories : System.Collections.Hashtable 376
{ 377
public VirtualDirectories(){} 378
//添加新的方法 379
public VirtualDirectory Find(string strName) 380
{ 381
return (VirtualDirectory)this[strName]; 382
} 383
} 384
}385


浙公网安备 33010602011771号