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