IIS目录操作类

View Code
  1 using System;
  2 using System.IO;
  3 using System.Text;
  4 using System.DirectoryServices;
  5 using System.Collections;
  6 
  7 namespace Mono.Web
  8 {
  9     /// <summary>
 10     /// IIS 虚拟目录操作类
 11     /// </summary>
 12     /// <remarks>
 13     /// <para>有关IIS的属性设置,请参考: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/html/bb9c0d25-d003-4ddd-8adb-8662de0a24ee.asp</para>
 14     /// <para>虚拟目录名区分大小写。</para>
 15     /// </remarks>
 16     public class VirtualDirectory : IDisposable
 17     {
 18         DirectoryEntry rootEntry; 
 19 
 20         /// <summary>
 21         /// 构造方法
 22         /// </summary>
 23         public VirtualDirectory()
 24         {
 25             rootEntry = new DirectoryEntry("IIS://localhost/W3SVC/1/root"); 
 26         }
 27 
 28         /// <summary>
 29         /// 构造方法
 30         /// </summary>
 31         /// <param name="path">路径(如: IIS://server1/W3SVC/1/root)</param>
 32         /// <param name="username">用户名</param>
 33         /// <param name="password">密码</param>
 34         public VirtualDirectory(string path, string username, string password)
 35         {
 36             rootEntry = new DirectoryEntry(path, username, password); 
 37         }
 38 
 39         /// <summary>
 40         /// 释放资源
 41         /// </summary>
 42         public void Dispose()
 43         {
 44             rootEntry.Close();
 45         }
 46 
 47         /// <summary>
 48         /// 是否存在
 49         /// </summary>
 50         /// <param name="name">虚拟目录名</param>
 51         /// <returns></returns>
 52         public bool Exists(string name) 
 53         { 
 54             bool exited =false; 
 55             DirectoryEntries entries = rootEntry.Children; 
 56             foreach(DirectoryEntry entry in entries) 
 57             { 
 58                 if(entry.Name == name) 
 59                     exited = true; 
 60             } 
 61 
 62             return exited; 
 63         }
 64 
 65         /// <summary>
 66         /// 创建
 67         /// </summary>
 68         /// <param name="name">虚拟目录名</param>
 69         /// <param name="path">对应物理路径</param>
 70         /// <returns></returns>
 71         public bool Create(string name, string path) 
 72         { 
 73             if (Exists(name)) return false;
 74 
 75             DirectoryEntry newVirDir = rootEntry.Children.Add(name, "IIsWebVirtualDir"); 
 76             newVirDir.Invoke("AppCreate", true); 
 77             newVirDir.CommitChanges(); 
 78             rootEntry .CommitChanges(); 
 79 
 80             newVirDir.Properties["AnonymousPasswordSync"].Value = true; 
 81             newVirDir.Properties["Path"].Value= path; 
 82             newVirDir.CommitChanges(); 
 83 
 84             return true;
 85         }
 86 
 87         /// <summary>
 88         /// 删除
 89         /// </summary>
 90         /// <param name="name">虚拟目录名</param>
 91         /// <returns></returns>
 92         public void Delete(string name) 
 93         { 
 94             if (!Exists(name)) return;
 95 
 96             rootEntry .Invoke("Delete", "IIsVirtualDir", name); 
 97             rootEntry .CommitChanges(); 
 98         } 
 99 
100         /// <summary>
101         /// 获取属性
102         /// </summary>
103         /// <param name="name">虚拟目录名</param>
104         /// <returns></returns>
105         /// <example>
106         /// VirtualDirectory vd = new VirtualDirectory();
107         /// DirectoryEntry entry = vd.GetProperties("Temp1");
108         /// foreach(string s in entry.Properties.PropertyNames)
109         /// {
110         ///        Console.WriteLine("{0}:{1} ({2})", s, entry.Properties[s].Value, entry.Properties[s].Value.GetType());
111         ///        foreach (object o in entry.Properties[s])
112         ///        {
113         ///            Console.WriteLine("\t{0}", o);
114         ///        }
115         /// }
116         /// </example>
117         public DirectoryEntry GetProperties(string name) 
118         { 
119             if (Exists(name)) 
120                 return rootEntry.Children.Find(name, "IIsWebVirtualDir"); 
121             else
122                 return null;
123         } 
124 
125         /// <summary>
126         /// 获取属性
127         /// </summary>
128         /// <param name="name">虚拟目录名</param>
129         /// <param name="property">属性名</param>
130         /// <returns></returns>
131         public object GetProperty(string name, string property)
132         {
133             DirectoryEntry info = GetProperties(name);
134             return info != null ? info.Properties[property].Value : null;
135         }
136 
137         /// <summary>
138         /// 设置属性
139         /// </summary>
140         /// <param name="name">虚拟目录名</param>
141         /// <param name="property">属性名</param>
142         /// <param name="value"></param>
143         public void SetProperty(string name, string property, object value)
144         {
145             if (Exists(name)) 
146             { 
147                 DirectoryEntry entry = rootEntry.Children.Find(name,"IIsWebVirtualDir"); 
148                 entry.Properties[property].Value = value;
149                 entry.CommitChanges();
150             } 
151         }
152 
153         /// <summary>
154         /// 设置默认文档
155         /// </summary>
156         /// <param name="name">虚拟目录名</param>
157         /// <param name="filename">文件名</param>
158         /// <example>
159         /// VirtualDirectory vd = new VirtualDirectory();
160         /// vd.SetDefaultDoc("test", "default.aspx", "index.aspx");
161         /// </example>
162         public void SetDefaultDoc(string name, params string[] filename)
163         {
164             StringBuilder sb = new StringBuilder();
165 
166             for(int i = 0; i < filename.Length; i++)
167             {
168                 sb.Append(filename[i]);
169                 if (i < filename.Length - 1) sb.Append(",");
170             }
171 
172             SetProperty(name, "DefaultDoc", sb.ToString());
173         }
174 
175         /// <summary>
176         /// 添加应用程序映射
177         /// </summary>
178         /// <param name="name">虚拟目录名</param>
179         /// <param name="ext">扩展名</param>
180         /// <param name="app">应用程序路径</param>
181         /// <param name="method">动作(OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE  为空时表示全部动作。)</param>
182         /// <param name="script">脚本引擎</param>
183         /// <param name="checkExists">检查文件是否存在</param>
184         public void AddScriptMap(string name, string ext, string app, string method, bool script, bool checkExists)
185         {
186             if (!ext.StartsWith(".")) 
187                 ext = "." + ext;
188             ext = ext.ToLower();
189 
190             int id = 0;
191             if (script && checkExists)
192                 id = 5;
193             else if (script)
194                 id = 1;
195             else if (checkExists)
196                 id = 4;
197 
198             string map = 
199                 method != null && method.Trim().Length > 0 ?
200                 string.Format("{0},{1},{2},{3}", ext, app, id, method) : 
201                 string.Format("{0},{1},{2}", ext, app, id);
202 
203             ArrayList maps = new ArrayList((object[])GetProperty(name, "ScriptMaps"));
204             
205             // Delete, if it's exists.
206             string ext2 = ext + ",";
207             for (int i  = 0; i < maps.Count; i++)
208             {
209                 if (((string)maps[i]).ToLower().StartsWith(ext))
210                 {
211                     maps.RemoveAt(i);
212                     break;
213                 }
214             }
215 
216             // Append
217             maps.Add(map);
218             SetProperty(name, "ScriptMaps", maps.ToArray(typeof(object)));
219         }
220 
221         /// <summary>
222         /// 删除应用程序映射
223         /// </summary>
224         /// <param name="name">虚拟目录名</param>
225         /// <param name="ext">扩展名</param>
226         public void DeleteScriptMap(string name, string ext)
227         {
228             if (!ext.StartsWith(".")) 
229                 ext = "." + ext;
230             ext = ext.ToLower();
231 
232             ArrayList maps = new ArrayList((object[])GetProperty(name, "ScriptMaps"));
233             
234             string ext2 = ext + ",";
235             for (int i  = 0; i < maps.Count; i++)
236             {
237                 if (((string)maps[i]).ToLower().StartsWith(ext))
238                 {
239                     maps.RemoveAt(i);
240                     
241                     SetProperty(name, "ScriptMaps", maps.ToArray(typeof(object)));
242                     return;
243                 }
244             }
245         }
246 
247         /// <summary>
248         /// 允许写入
249         /// </summary>
250         /// <param name="name">虚拟目录名</param>
251         public void EnabledAccessWrite(string name)
252         {
253             SetProperty(name, "AccessFlags", (int)GetProperty(name, "AccessFlags") | 0x00000002);
254         }
255 
256         /// <summary>
257         /// 允许目录浏览
258         /// </summary>
259         /// <param name="name">虚拟目录名</param>
260         public void EnabledDirBrowse(string name)
261         {
262             SetProperty(name, "DirBrowseFlags", (int)GetProperty(name, "DirBrowseFlags") | 0x80000000);
263         }
264 
265         /// <summary>
266         /// 设置路径
267         /// </summary>
268         /// <param name="name">虚拟目录名</param>
269         /// <param name="path">物理路径</param>
270         public void SetPath(string name, string path)
271         {
272             SetProperty(name, "Path", path);
273         }
274 
275         /// <summary>
276         /// 设置会话超时
277         /// </summary>
278         /// <param name="name">虚拟目录名</param>
279         /// <param name="path">超时时间(分钟)</param>
280         public void SetSessionTimeout(string name, int timeout)
281         {
282             SetProperty(name, "AspSessionTimeout", timeout);
283         }
284 
285         /// <summary>
286         /// 设置脚本超时
287         /// </summary>
288         /// <param name="name">虚拟目录名</param>
289         /// <param name="path">超时时间(秒)</param>
290         public void SetScriptTimeout(string name, int timeout)
291         {
292             SetProperty(name, "AspScriptTimeout", timeout);
293         }
294     }
295 }

 

posted @ 2013-03-23 22:48  青空feng  阅读(232)  评论(0编辑  收藏  举报