1. ///***********************************************************  
  2. ///************** IIS控制管理类 1.0 Beta **************  
  3. ///************** Author: 飞刀 **************  
  4. ///**************http://www.aspcn.com **************  
  5. ///************** feidao@aspcn.com **************  
  6. ///***********************************************************  
  7. using System;  
  8. using System.Data;  
  9. using System.DirectoryServices;  
  10. using System.Collections;  
  11. namespace Aspcn.Management  
  12. {  
  13. /// <summary>  
  14. /// IISManager 的摘要说明。  
  15. /// </summary>  
  16. public class IISManager  
  17. {  
  18. //定义需要使用的  
  19. private string _server,_website;  
  20. private VirtualDirectories _virdirs;  
  21. protected System.DirectoryServices.DirectoryEntry rootfolder;  
  22. private bool _batchflag;  
  23. public IISManager()  
  24. {  
  25. //默认情况下使用localhost,即访问本地机  
  26. _server = "localhost";  
  27. _website = "1";  
  28. _batchflag = false;  
  29. }  
  30. public IISManager(string strServer)  
  31. {  
  32. _server = strServer;  
  33. _website = "1";   
  34. _batchflag = false;  
  35. }  
  36. /// <summary>  
  37. /// 定义公共属性  
  38. /// </summary>  
  39.  
  40. //Server属性定义访问机器的名字,可以是IP与计算名  
  41. public string Server  
  42. {  
  43. getreturn _server;}  
  44. set{ _server = value;}  
  45. }  
  46. //WebSite属性定义,为一数字,为方便,使用string   
  47. //一般来说第一台主机为1,第二台主机为2,依次类推  
  48. public string WebSite  
  49. {  
  50. getreturn _website; }  
  51. set{ _website = value; }  
  52. }  
  53.  
  54. //虚拟目录的名字  
  55. public VirtualDirectories VirDirs  
  56. {  
  57. getreturn _virdirs; }  
  58. set{ _virdirs = value;}  
  59. }  
  60. ///<summary>  
  61. ///定义公共方法  
  62. ///</summary>  
  63.  
  64. //连接服务器  
  65. public void Connect()  
  66. {  
  67. ConnectToServer();  
  68. }  
  69. //为方便重载  
  70. public void Connect(string strServer)  
  71. {  
  72. _server = strServer;  
  73. ConnectToServer();  
  74. }  
  75. //为方便重载  
  76. public void Connect(string strServer,string strWebSite)  
  77. {  
  78. _server = strServer;  
  79. _website = strWebSite;  
  80. ConnectToServer();  
  81. }  
  82. //判断是否存这个虚拟目录  
  83. public bool Exists(string strVirdir)  
  84. {  
  85. return _virdirs.Contains(strVirdir);  
  86. }  
  87. //添加一个虚拟目录  
  88. public void Create(VirtualDirectory newdir)  
  89. {  
  90. string strPath = "IIS://" + _server + "/W3SVC/" + _website + "/ROOT/" + newdir.Name;  
  91. if(!_virdirs.Contains(newdir.Name) || _batchflag )  
  92. {  
  93. try 
  94. {  
  95. //加入到ROOT的Children集合中去  
  96. DirectoryEntry newVirDir = rootfolder.Children.Add(newdir.Name,"IIsWebVirtualDir");  
  97. newVirDir.Invoke("AppCreate",true);  
  98. newVirDir.CommitChanges();  
  99. rootfolder.CommitChanges();  
  100. //然后更新数据  
  101. UpdateDirInfo(newVirDir,newdir);  
  102. }  
  103. catch(Exception ee)  
  104. {  
  105. throw new Exception(ee.ToString());  
  106. }  
  107. }  
  108. else 
  109. {  
  110. throw new Exception("This virtual directory is already exist.");  
  111. }  
  112. }  
  113. //得到一个虚拟目录  
  114. public VirtualDirectory GetVirDir(string strVirdir)  
  115. {  
  116. VirtualDirectory tmp = null;  
  117. if(_virdirs.Contains(strVirdir))  
  118. {  
  119. tmp = _virdirs.Find(strVirdir);  
  120. ((VirtualDirectory)_virdirs[strVirdir]).flag = 2;  
  121. }  
  122. else 
  123. {  
  124. throw new Exception("This virtual directory is not exists");  
  125. }  
  126. return tmp;  
  127. }  
  128.  
  129. //更新一个虚拟目录  
  130. public void Update(VirtualDirectory dir)  
  131. {  
  132. //判断需要更改的虚拟目录是否存在  
  133. if(_virdirs.Contains(dir.Name))  
  134. {  
  135. DirectoryEntry ode = rootfolder.Children.Find(dir.Name,"IIsWebVirtualDir");  
  136. UpdateDirInfo(ode,dir);  
  137. }  
  138. else 
  139. {  
  140. throw new Exception("This virtual directory is not exists.");  
  141. }  
  142. }  
  143.    
  144. //删除一个虚拟目录  
  145. public void Delete(string strVirdir)  
  146. {  
  147. if(_virdirs.Contains(strVirdir))  
  148. {  
  149. object[] paras = new object[2];  
  150. paras[0] = "IIsWebVirtualDir"//表示操作的是虚拟目录  
  151. paras[1] = strVirdir;  
  152. rootfolder.Invoke("Delete",paras);  
  153. rootfolder.CommitChanges();  
  154. }  
  155. else 
  156. {  
  157. throw new Exception("Can''t delete " + strVirdir + ",because it isn''t exists.");  
  158. }  
  159. }  
  160. //批量更新  
  161. public void UpdateBatch()  
  162. {  
  163. BatchUpdate(_virdirs);  
  164. }  
  165. //重载一个:-)  
  166. public void UpdateBatch(VirtualDirectories vds)  
  167. {  
  168. BatchUpdate(vds);  
  169. }  
  170.    
  171. ///<summary>  
  172. ///私有方法  
  173. ///</summary>  
  174.  
  175. //连接服务器  
  176. private void ConnectToServer()  
  177. {  
  178. string strPath = "IIS://" + _server + "/W3SVC/" + _website +"/ROOT";  
  179. try 
  180. {  
  181. this.rootfolder = new DirectoryEntry(strPath);  
  182. _virdirs = GetVirDirs(this.rootfolder.Children);  
  183. }   
  184. catch(Exception e)  
  185. {  
  186. throw new Exception("Can''t connect to the server ["+ _server +"] ...",e);  
  187. }  
  188. }  
  189. //执行批量更新  
  190. private void BatchUpdate(VirtualDirectories vds)  
  191. {  
  192. _batchflag = true;  
  193. foreach(object item in vds.Values)  
  194. {  
  195. VirtualDirectory vd = (VirtualDirectory)item;  
  196. switch(vd.flag)  
  197. {  
  198. case 0:  
  199. break;  
  200. case 1:  
  201. Create(vd);  
  202. break;  
  203. case 2:  
  204. Update(vd);  
  205. break;  
  206. }  
  207. }  
  208. _batchflag = false;  
  209. }  
  210. //更新东东  
  211. private void UpdateDirInfo(DirectoryEntry de,VirtualDirectory vd)  
  212. {  
  213. de.Properties["AnonymousUserName"][0] = vd.AnonymousUserName;  
  214. de.Properties["AnonymousUserPass"][0] = vd.AnonymousUserPass;  
  215. de.Properties["AccessRead"][0] = vd.AccessRead;  
  216. de.Properties["AccessExecute"][0] = vd.AccessExecute;  
  217. de.Properties["AccessWrite"][0] = vd.AccessWrite;  
  218. de.Properties["AuthBasic"][0] = vd.AuthBasic;  
  219. de.Properties["AuthNTLM"][0] = vd.AuthNTLM;  
  220. de.Properties["ContentIndexed"][0] = vd.ContentIndexed;  
  221. de.Properties["EnableDefaultDoc"][0] = vd.EnableDefaultDoc;  
  222. de.Properties["EnableDirBrowsing"][0] = vd.EnableDirBrowsing;  
  223. de.Properties["AccessSSL"][0] = vd.AccessSSL;  
  224. de.Properties["AccessScript"][0] = vd.AccessScript;  
  225. de.Properties["DefaultDoc"][0] = vd.DefaultDoc;  
  226. de.Properties["Path"][0] = vd.Path;  
  227. de.CommitChanges();  
  228. }  
  229.  
  230. //获取虚拟目录集合  
  231. private VirtualDirectories GetVirDirs(DirectoryEntries des)  
  232. {  
  233. VirtualDirectories tmpdirs = new VirtualDirectories();  
  234. foreach(DirectoryEntry de in des)  
  235. {  
  236. if(de.SchemaClassName == "IIsWebVirtualDir")  
  237. {  
  238. VirtualDirectory vd = new VirtualDirectory();  
  239. vd.Name = de.Name;  
  240. vd.AccessRead = (bool)de.Properties["AccessRead"][0];  
  241. vd.AccessExecute = (bool)de.Properties["AccessExecute"][0];  
  242. vd.AccessWrite = (bool)de.Properties["AccessWrite"][0];  
  243. vd.AnonymousUserName = (string)de.Properties["AnonymousUserName"][0];  
  244. vd.AnonymousUserPass = (string)de.Properties["AnonymousUserName"][0];  
  245. vd.AuthBasic = (bool)de.Properties["AuthBasic"][0];  
  246. vd.AuthNTLM = (bool)de.Properties["AuthNTLM"][0];  
  247. vd.ContentIndexed = (bool)de.Properties["ContentIndexed"][0];  
  248. vd.EnableDefaultDoc = (bool)de.Properties["EnableDefaultDoc"][0];  
  249. vd.EnableDirBrowsing = (bool)de.Properties["EnableDirBrowsing"][0];  
  250. vd.AccessSSL = (bool)de.Properties["AccessSSL"][0];  
  251. vd.AccessScript = (bool)de.Properties["AccessScript"][0];  
  252. vd.Path = (string)de.Properties["Path"][0];  
  253. vd.flag = 0;  
  254. vd.DefaultDoc = (string)de.Properties["DefaultDoc"][0];  
  255. tmpdirs.Add(vd.Name,vd);  
  256. }  
  257. }  
  258. return tmpdirs;  
  259. }  
  260.  
  261. }  
  262. /// <summary>  
  263. /// VirtualDirectory类  
  264. /// </summary>  
  265. public class VirtualDirectory  
  266. {  
  267. private bool _read,_execute,_script,_ssl,_write,_authbasic,_authntlm,_indexed,_endirbrow,_endefaultdoc;  
  268. private string _ausername,_auserpass,_name,_path;  
  269. private int _flag;  
  270. private string _defaultdoc;  
  271. /// <summary>  
  272. /// 构造函数  
  273. /// </summary>  
  274. public VirtualDirectory()  
  275. {  
  276. SetValue();  
  277. }  
  278. public VirtualDirectory(string strVirDirName)  
  279. {  
  280. _name = strVirDirName;  
  281. SetValue();  
  282. }  
  283. private void SetValue()  
  284. {  
  285. _read = true;_execute = false;_script = false;_ssl= false;_write=false;_authbasic=false;_authntlm=false;  
  286. _indexed = false;_endirbrow=false;_endefaultdoc = false;  
  287. _flag = 1;  
  288. _defaultdoc = "default.htm,default.aspx,default.asp,index.htm";  
  289. _path = "C:\\";  
  290. _ausername = "";_auserpass ="";_name="";  
  291. }  
  292. ///<summary>  
  293. ///定义属性,IISVirtualDir太多属性了  
  294. ///我只搞了比较重要的一些,其它的大伙需要的自个加吧。  
  295. ///</summary>  
  296.  
  297. public int flag  
  298. {  
  299. getreturn _flag;}  
  300. set{ _flag = value;}  
  301. }  
  302. public bool AccessRead  
  303. {  
  304. getreturn _read;}  
  305. set{ _read = value;}  
  306. }  
  307. public bool AccessWrite  
  308. {  
  309. getreturn _write;}  
  310. set{ _write = value;}  
  311. }  
  312. public bool AccessExecute  
  313. {  
  314. getreturn _execute;}  
  315. set{ _execute = value;}  
  316. }  
  317. public bool AccessSSL  
  318. {  
  319. getreturn _ssl;}  
  320. set{ _ssl = value;}  
  321. }  
  322. public bool AccessScript  
  323. {  
  324. getreturn _script;}  
  325. set{ _script = value;}  
  326. }  
  327. public bool AuthBasic  
  328. {  
  329. getreturn _authbasic;}  
  330. set{ _authbasic = value;}  
  331. }  
  332. public bool AuthNTLM  
  333. {  
  334. getreturn _authntlm;}  
  335. set{ _authntlm = value;}  
  336. }  
  337. public bool ContentIndexed  
  338. {  
  339. getreturn _indexed;}  
  340. set{ _indexed = value;}  
  341. }  
  342. public bool EnableDirBrowsing  
  343. {  
  344. getreturn _endirbrow;}  
  345. set{ _endirbrow = value;}  
  346. }  
  347. public bool EnableDefaultDoc  
  348. {  
  349. getreturn _endefaultdoc;}  
  350. set{ _endefaultdoc = value;}  
  351. }  
  352. public string Name  
  353. {  
  354. getreturn _name;}  
  355. set{ _name = value;}  
  356. }  
  357. public string Path  
  358. {  
  359. getreturn _path;}  
  360. set{ _path = value;}  
  361. }  
  362. public string DefaultDoc  
  363. {  
  364. getreturn _defaultdoc;}  
  365. set{ _defaultdoc = value;}  
  366. }  
  367. public string AnonymousUserName  
  368. {  
  369. getreturn _ausername;}  
  370. set{ _ausername = value;}  
  371. }  
  372. public string AnonymousUserPass  
  373. {  
  374. getreturn _auserpass;}  
  375. set{ _auserpass = value;}  
  376. }  
  377. }  
  378. /// <summary>  
  379. /// 集合VirtualDirectories  
  380. /// </summary>  
  381.  
  382. public class VirtualDirectories : System.Collections.Hashtable  
  383. {  
  384. public VirtualDirectories()  
  385. {  
  386. }  
  387. //添加新的方法   
  388. public VirtualDirectory Find(string strName)  
  389. {  
  390. return (VirtualDirectory)this[strName];  
  391. }  
  392. }  
  393. }   
  394.  
  395. //***********************************************************  
  396. // 引用未经测试  
  397. //***********************************************************  
此文章由 flyinweb 于 2010-11-08 18:02:37 编辑

本日志由 flyinweb 于 2009-06-18 19:13:25 发表,目前已经被浏览 4104 次,评论 0 次;

作者添加了以下标签: C#IIS管理

引用通告:http://www.517sou.net/Article/22/Trackback.ashx

评论订阅:http://www.517sou.net/Article/22/Feeds.ashx

相关文章

评论列表

    暂时没有评论
(必填)
(必填,不会被公开)