• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

冉冉

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

.Net中操作IIS源代码

  1
  2using System;
  3  using System.Data;
  4  using System.DirectoryServices;
  5  using System.Collections;
  6  namespace Aspcn.Management
  7  {
  8  /**//// <summary>
  9  /// IISManager 的摘要说明。
 10  /// </summary>

 11  public class IISManager
 12  {
 13  //定义需要使用的
 14  private string _server,_website;
 15  private VirtualDirectories _virdirs;
 16  protected System.DirectoryServices.DirectoryEntry rootfolder;
 17  private bool _batchflag;
 18  public IISManager()
 19  {
 20  //默认情况下使用localhost,即访问本地机
 21  _server = "localhost";
 22  _website = "1";
 23  _batchflag = false;
 24  }

 25  public IISManager(string strServer)
 26  {
 27  _server = strServer;
 28  _website = "1";
 29  _batchflag = false;
 30  }

 31  /**//// <summary>
 32  /// 定义公共属性
 33  /// </summary>

 34
 35  //Server属性定义访问机器的名字,可以是IP与计算名
 36  public string Server
 37  {
 38  get{ return _server;}
 39  set{ _server = value;}
 40  }

 41  //WebSite属性定义,为一数字,为方便,使用string
 42  //一般来说第一台主机为1,第二台主机为2,依次类推
 43  public string WebSite
 44  {
 45  get{ return _website; }
 46  set{ _website = value; }
 47  }

 48
 49  //虚拟目录的名字
 50  public VirtualDirectories VirDirs
 51  {
 52  get{ return _virdirs; }
 53  set{ _virdirs = value;}
 54  }

 55  /**////<summary>
 56  ///定义公共方法
 57  ///</summary>

 58
 59  //连接服务器
 60  public void Connect()
 61  {
 62  ConnectToServer();
 63  }

 64  //为方便重载
 65  public void Connect(string strServer)
 66  {
 67  _server = strServer;
 68  ConnectToServer();
 69  }

 70  //为方便重载
 71  public void Connect(string strServer,string strWebSite)
 72  {
 73  _server = strServer;
 74  _website = strWebSite;
 75  ConnectToServer();
 76  }

 77  //判断是否存这个虚拟目录
 78  public bool Exists(string strVirdir)
 79  {
 80  return _virdirs.Contains(strVirdir);
 81  }

 82  //添加一个虚拟目录
 83  public void Create(VirtualDirectory newdir)
 84  {
 85  string strPath = "IIS://" + _server + "/W3SVC/" + _website + "/ROOT/" + newdir.Name;
 86  if(!_virdirs.Contains(newdir.Name) || _batchflag )
 87  {
 88  try
 89  {
 90  //加入到ROOT的Children集合中去
 91  DirectoryEntry newVirDir = rootfolder.Children.Add(newdir.Name,"IIsWebVirtualDir");
 92  newVirDir.Invoke("AppCreate",true);
 93  newVirDir.CommitChanges();
 94  rootfolder.CommitChanges();
 95  //然后更新数据
 96  UpdateDirInfo(newVirDir,newdir);
 97  }

 98  catch(Exception ee)
 99  {
100  throw new Exception(ee.ToString());
101  }

102  }

103  else
104  {
105  throw new Exception("This virtual directory is already exist.");
106  }

107  }

108  //得到一个虚拟目录
109  public VirtualDirectory GetVirDir(string strVirdir)
110  {
111  VirtualDirectory tmp = null;
112  if(_virdirs.Contains(strVirdir))
113  {
114  tmp = _virdirs.Find(strVirdir);
115  ((VirtualDirectory)_virdirs[strVirdir]).flag = 2;
116  }

117  else
118  {
119  throw new Exception("This virtual directory is not exists");
120  }

121  return tmp;
122  }

123
124  //更新一个虚拟目录
125  public void Update(VirtualDirectory dir)
126  {
127  //判断需要更改的虚拟目录是否存在
128  if(_virdirs.Contains(dir.Name))
129  {
130  DirectoryEntry ode = rootfolder.Children.Find(dir.Name,"IIsWebVirtualDir");
131  UpdateDirInfo(ode,dir);
132  }

133  else
134  {
135  throw new Exception("This virtual directory is not exists.");
136  }

137  }

138   
139  //删除一个虚拟目录
140  public void Delete(string strVirdir)
141  {
142  if(_virdirs.Contains(strVirdir))
143  {
144  object[] paras = new object[2];
145  paras[0] = "IIsWebVirtualDir"; //表示操作的是虚拟目录
146  paras[1] = strVirdir;
147  rootfolder.Invoke("Delete",paras);
148  rootfolder.CommitChanges();
149  }

150  else
151  {
152  throw new Exception("Can't delete " + strVirdir + ",because it isn't exists.");
153  }

154  }

155  //批量更新
156  public void UpdateBatch()
157  {
158  BatchUpdate(_virdirs);
159  }

160  //重载一个:-)
161  public void UpdateBatch(VirtualDirectories vds)
162  {
163  BatchUpdate(vds);
164  }

165   
166  /**////<summary>
167  ///私有方法
168  ///</summary>

169
170  //连接服务器
171  private void ConnectToServer()
172  {
173  string strPath = "IIS://" + _server + "/W3SVC/" + _website +"/ROOT";
174  try
175  {
176  this.rootfolder = new DirectoryEntry(strPath);
177  _virdirs = GetVirDirs(this.rootfolder.Children);
178  }

179  catch(Exception e)
180  {
181  throw new Exception("Can't connect to the server ["+ _server +"] ",e);
182  }

183  }

184  //执行批量更新
185  private void BatchUpdate(VirtualDirectories vds)
186  {
187  _batchflag = true;
188  foreach(object item in vds.Values)
189  {
190  VirtualDirectory vd = (VirtualDirectory)item;
191  switch(vd.flag)
192  {
193  case 0:
194  break;
195  case 1:
196  Create(vd);
197  break;
198  case 2:
199  Update(vd);
200  break;
201  }

202  }

203  _batchflag = false;
204  }

205  //更新东东
206  private void UpdateDirInfo(DirectoryEntry de,VirtualDirectory vd)
207  {
208  de.Properties["AnonymousUserName"][0] = vd.AnonymousUserName;
209  de.Properties["AnonymousUserPass"][0] = vd.AnonymousUserPass;
210  de.Properties["AccessRead"][0] = vd.AccessRead;
211  de.Properties["AccessExecute"][0] = vd.AccessExecute;
212  de.Properties["AccessWrite"][0] = vd.AccessWrite;
213  de.Properties["AuthBasic"][0] = vd.AuthBasic;
214  de.Properties["AuthNTLM"][0] = vd.AuthNTLM;
215  de.Properties["ContentIndexed"][0] = vd.ContentIndexed;
216  de.Properties["EnableDefaultDoc"][0] = vd.EnableDefaultDoc;
217  de.Properties["EnableDirBrowsing"][0] = vd.EnableDirBrowsing;
218  de.Properties["AccessSSL"][0] = vd.AccessSSL;
219  de.Properties["AccessScript"][0] = vd.AccessScript;
220  de.Properties["DefaultDoc"][0] = vd.DefaultDoc;
221  de.Properties["Path"][0] = vd.Path;
222  de.CommitChanges();
223  }

224
225  //获取虚拟目录集合
226  private VirtualDirectories GetVirDirs(DirectoryEntries des)
227  {
228  VirtualDirectories tmpdirs = new VirtualDirectories();
229  foreach(DirectoryEntry de in des)
230  {
231  if(de.SchemaClassName == "IIsWebVirtualDir")
232  {
233  VirtualDirectory vd = new VirtualDirectory();
234  vd.Name = de.Name;
235  vd.AccessRead = (bool)de.Properties["AccessRead"][0];
236  vd.AccessExecute = (bool)de.Properties["AccessExecute"][0];
237  vd.AccessWrite = (bool)de.Properties["AccessWrite"][0];
238  vd.AnonymousUserName = (string)de.Properties["AnonymousUserName"][0];
239  vd.AnonymousUserPass = (string)de.Properties["AnonymousUserName"][0];
240  vd.AuthBasic = (bool)de.Properties["AuthBasic"][0];
241  vd.AuthNTLM = (bool)de.Properties["AuthNTLM"][0];
242  vd.ContentIndexed = (bool)de.Properties["ContentIndexed"][0];
243  vd.EnableDefaultDoc = (bool)de.Properties["EnableDefaultDoc"][0];
244  vd.EnableDirBrowsing = (bool)de.Properties["EnableDirBrowsing"][0];
245  vd.AccessSSL = (bool)de.Properties["AccessSSL"][0];
246  vd.AccessScript = (bool)de.Properties["AccessScript"][0];
247  vd.Path = (string)de.Properties["Path"][0];
248  vd.flag = 0;
249  vd.DefaultDoc = (string)de.Properties["DefaultDoc"][0];
250  tmpdirs.Add(vd.Name,vd);
251  }

252  }

253  return tmpdirs;
254  }

255
256  }

257  /**//// <summary>
258  /// VirtualDirectory类
259  /// </summary>

260  public class VirtualDirectory
261  {
262  private bool _read,_execute,_script,_ssl,_write,_authbasic,_authntlm,_indexed,_endirbrow,_endefaultdoc;
263  private string _ausername,_auserpass,_name,_path;
264  private int _flag;
265  private string _defaultdoc;
266  /**//// <summary>
267  /// 构造函数
268  /// </summary>

269  public VirtualDirectory()
270  {
271  SetValue();
272  }

273  public VirtualDirectory(string strVirDirName)
274  {
275  _name = strVirDirName;
276  SetValue();
277  }

278  private void SetValue()
279  {
280  _read = true;_execute = false;_script = false;_ssl= false;_write=false;_authbasic=false;_authntlm=false;
281  _indexed = false;_endirbrow=false;_endefaultdoc = false;
282  _flag = 1;
283  _defaultdoc = "default.htm,default.aspx,default.asp,index.htm";
284  _path = "C:\\";
285  _ausername = "";_auserpass ="";_name="";
286  }

287  /**////<summary>
288  ///定义属性,IISVirtualDir太多属性了
289  ///我只搞了比较重要的一些,其它的大伙需要的自个加吧。
290  ///</summary>

291
292  public int flag
293  {
294  get{ return _flag;}
295  set{ _flag = value;}
296  }

297  public bool AccessRead
298  {
299  get{ return _read;}
300  set{ _read = value;}
301  }

302  public bool AccessWrite
303  {
304  get{ return _write;}
305  set{ _write = value;}
306  }

307  public bool AccessExecute
308  {
309  get{ return _execute;}
310  set{ _execute = value;}
311  }

312  public bool AccessSSL
313  {
314  get{ return _ssl;}
315  set{ _ssl = value;}
316  }

317  public bool AccessScript
318  {
319  get{ return _script;}
320  set{ _script = value;}
321  }

322  public bool AuthBasic
323  {
324  get{ return _authbasic;}
325  set{ _authbasic = value;}
326  }

327  public bool AuthNTLM
328  {
329  get{ return _authntlm;}
330  set{ _authntlm = value;}
331  }

332  public bool ContentIndexed
333  {
334  get{ return _indexed;}
335  set{ _indexed = value;}
336  }

337  public bool EnableDirBrowsing
338  {
339  get{ return _endirbrow;}
340  set{ _endirbrow = value;}
341  }

342  public bool EnableDefaultDoc
343  {
344  get{ return _endefaultdoc;}
345  set{ _endefaultdoc = value;}
346  }

347  public string Name
348  {
349  get{ return _name;}
350  set{ _name = value;}
351  }

352  public string Path
353  {
354  get{ return _path;}
355  set{ _path = value;}
356  }

357  public string DefaultDoc
358  {
359  get{ return _defaultdoc;}
360  set{ _defaultdoc = value;}
361  }

362  public string AnonymousUserName
363  {
364  get{ return _ausername;}
365  set{ _ausername = value;}
366  }

367  public string AnonymousUserPass
368  {
369  get{ return _auserpass;}
370  set{ _auserpass = value;}
371  }

372  }

373  /**//// <summary>
374  /// 集合VirtualDirectories
375  /// </summary>

376
377  public class VirtualDirectories : System.Collections.Hashtable
378  {
379  public VirtualDirectories()
380  {
381  }

382  //添加新的方法
383  public VirtualDirectory Find(string strName)
384  {
385  return (VirtualDirectory)this[strName];
386  }

387  }

388  }

posted on 2006-09-07 09:32  冉冉  阅读(234)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3