默认根目录为Default Web Site. private void button1_Click(object sender, EventArgs e) { //创建虚拟目录 this.creatVirtualDirectory("IIS://localhost/W3SVC/1/ROOT","c://SOM","HSM221"); } private void button2_Click(object sender, EventArgs e) { this.DelVirtualDirectory("IIS://localhost/W3SVC/1/ROOT", "HSM221"); } //创建虚拟目录 private void creatVirtualDirectory(string WebSiteName, string physicalPath, string virtualDirName) { //const String constIISWebSiteRoot = "IIS://localhost/W3SVC/1/ROOT"; String constIISWebSiteRoot = WebSiteName; // 比如 "IIS://localhost/W3SVC/1/ROOT"; DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot); DirectoryEntry tbEntry = root.Children.Add(virtualDirName, "IIsWebVirtualDir"); //must be end with a '/' tbEntry.Properties["Path"][0] = physicalPath; tbEntry.Invoke("AppCreate",true); tbEntry.Properties["AccessRead"][0] = true; tbEntry.Properties["ContentIndexed"][0] = false; tbEntry.Properties["DefaultDoc"][0] = "index.php"; tbEntry.Properties["AppFriendlyName"][0] = virtualDirName; tbEntry.Properties["AuthFlags"][0] = 1; // 设置目录的安全性,0表示不允许匿名访问,1为允许,3为基本身份验证,7为windows继承身份验证 tbEntry.Properties["AppIsolated"][0] = 2; tbEntry.Properties["AccessScript"][0] = true; tbEntry.Properties["DontLog"][0] = true; tbEntry.CommitChanges(); root.CommitChanges(); root.Close(); } //删除虚拟目录是这样的 private int DelVirtualDirectory(string WebSiteName, string VirtualDirectory) { try { if (!GetVirtualDirectory(WebSiteName,VirtualDirectory)) { MessageBox.Show("error:该虚拟目录不存在."); return 3;// "error:该虚拟目录不存在."; } DirectoryEntry deRoot = new DirectoryEntry(WebSiteName); deRoot.RefreshCache(); DirectoryEntry Dirport = deRoot.Children.Find(VirtualDirectory, "IIsWebVirtualDir"); deRoot.Children.Remove(Dirport); deRoot.CommitChanges(); deRoot.Close(); MessageBox.Show("成功"); return 1; // "successful:删除虚拟目录" + nameDirectory + "成功!"; } catch (Exception e) { MessageBox.Show("失败" + e.Message); return 0; // "error:删除目录失败." + e.Message; } } //查找对应的虚拟站点. private string GetSiteID(string WebSiteName) { DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC"); try { string SiteID = null; string hostname; foreach (DirectoryEntry bb in root.Children) { try { //if(Convert.ToInt32(bb.Name.Trim()) < 0) continue; PropertyValueCollection pvc = bb.Properties["ServerBindings"]; String[] srvBindings = ((string)pvc[0]).Split(new char[] { ':' }); hostname = srvBindings[2].Trim(); //判断,可换用hostname //if (WebSiteName == bb.Properties["ServerComment"].Value.ToString()) SiteID=bb.Name; if (WebSiteName == hostname) SiteID = bb.Name; // Clear Variable hostname = ""; } catch { } } if (SiteID == null) return null; return SiteID; } catch { return null; } } //查找对应的虚拟目录 private Boolean GetVirtualDirectory(string WebSiteName,string VirtualDirectory) { //DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC/1/ROOT"); /* DirectoryEntry entry = rootEntry.Children.Find(this.Alias, "IIsWebVirtualDir"); return(entry != null); */ DirectoryEntry root = new DirectoryEntry(WebSiteName); try { foreach (DirectoryEntry tmpDir in root.Children) { try { if (VirtualDirectory == tmpDir.Name) { return true; } } catch { } } return false; } catch { return false; } }