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 ///10 /// IIS 虚拟目录操作类 11 /// 12 ///13 /// 16 public class VirtualDirectory : IDisposable 17 { 18 DirectoryEntry rootEntry; 19 20 ///有关IIS的属性设置,请参考: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/html/bb9c0d25-d003-4ddd-8adb-8662de0a24ee.asp 14 ///虚拟目录名区分大小写。 15 ///21 /// 构造方法 22 /// 23 public VirtualDirectory() 24 { 25 rootEntry = new DirectoryEntry("IIS://localhost/W3SVC/1/root"); 26 } 27 28 ///29 /// 构造方法 30 /// 31 /// 路径(如: IIS://server1/W3SVC/1/root) 32 /// 用户名 33 /// 密码 34 public VirtualDirectory(string path, string username, string password) 35 { 36 rootEntry = new DirectoryEntry(path, username, password); 37 } 38 39 ///40 /// 释放资源 41 /// 42 public void Dispose() 43 { 44 rootEntry.Close(); 45 } 46 47 ///48 /// 是否存在 49 /// 50 /// 虚拟目录名 51 ///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 /// 66 /// 创建 67 /// 68 /// 虚拟目录名 69 /// 对应物理路径 70 ///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 /// 88 /// 删除 89 /// 90 /// 虚拟目录名 91 ///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 /// 101 /// 获取属性102 /// 103 /// 虚拟目录名104 ///105 /// 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 /// 117 public DirectoryEntry GetProperties(string name) 118 { 119 if (Exists(name)) 120 return rootEntry.Children.Find(name, "IIsWebVirtualDir"); 121 else122 return null;123 } 124 125 ///126 /// 获取属性127 /// 128 /// 虚拟目录名129 /// 属性名130 ///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 /// 138 /// 设置属性139 /// 140 /// 虚拟目录名141 /// 属性名142 /// 值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 ///154 /// 设置默认文档155 /// 156 /// 虚拟目录名157 /// 文件名158 ///159 /// VirtualDirectory vd = new VirtualDirectory();160 /// vd.SetDefaultDoc("test", "default.aspx", "index.aspx");161 /// 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 ///176 /// 添加应用程序映射177 /// 178 /// 虚拟目录名179 /// 扩展名180 /// 应用程序路径181 /// 动作(OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE 为空时表示全部动作。)182 /// 脚本引擎183 /// 检查文件是否存在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 // Append217 maps.Add(map);218 SetProperty(name, "ScriptMaps", maps.ToArray(typeof(object)));219 }220 221 ///222 /// 删除应用程序映射223 /// 224 /// 虚拟目录名225 /// 扩展名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 ///248 /// 允许写入249 /// 250 /// 虚拟目录名251 public void EnabledAccessWrite(string name)252 {253 SetProperty(name, "AccessFlags", (int)GetProperty(name, "AccessFlags") | 0x00000002);254 }255 256 ///257 /// 允许目录浏览258 /// 259 /// 虚拟目录名260 public void EnabledDirBrowse(string name)261 {262 SetProperty(name, "DirBrowseFlags", (int)GetProperty(name, "DirBrowseFlags") | 0x80000000);263 }264 265 ///266 /// 设置路径267 /// 268 /// 虚拟目录名269 /// 物理路径270 public void SetPath(string name, string path)271 {272 SetProperty(name, "Path", path);273 }274 275 ///276 /// 设置会话超时277 /// 278 /// 虚拟目录名279 /// 超时时间(分钟)280 public void SetSessionTimeout(string name, int timeout)281 {282 SetProperty(name, "AspSessionTimeout", timeout);283 }284 285 ///286 /// 设置脚本超时287 /// 288 /// 虚拟目录名289 /// 超时时间(秒)290 public void SetScriptTimeout(string name, int timeout)291 {292 SetProperty(name, "AspScriptTimeout", timeout);293 }294 }295 }