今天就來小記一下學習的心得吧!! .net 提供了一組好用的api ADSI 可以去管理IIS相關設定。 例如: 建立虛擬目錄, 設定 extension mapping 等。 因為工作上的需要,我只針對 建 virtual path跟 extension mapping 去實作。 ADSI--> IIS://電腦名稱/服務/網站/路徑
所以只要知道,你想要使用者什麼服務如WEB SERVER,或FTP SERVER等,再加上網站跟路徑就可以逶過這組API 對IIS的相關設定去做操作。 code:
using System.DirectoryServices; class IISinfo { private string VirPath; private string ADSI; public IISinfo(){ this.ADSI = String.Format("IIS://{0}/w3svc/{1}/root", "localhost", "1"); } //Create Extension mapping public void AddScriptMaps(string vpath, string extension_exePath) { this.VirPath = vpath; DirectoryEntry VDir = new DirectoryEntry(this.ADSI+"/"+this.VirPath); ArrayList mapping = new ArrayList(); PropertyValueCollection pvc = VDir.Properties["ScriptMaps"]; foreach (object Value in pvc) mapping.Add(Value.ToString()); mapping.Add(extension_exePath); VDir.Properties["ScriptMaps"].Value = mapping.ToArray(); VDir.CommitChanges(); } //Create Virtual path public void CreateNewVP(string vpath, string phyPath) { bool VirExist = false; this.VirPath = vpath; DirectoryEntry VDir = new DirectoryEntry(this.ADSI); try { //Check if VDir is exists foreach (DirectoryEntry subVD in VDir.Children) { if (subVD.SchemaClassName == "IIsWebVirtualDir" && subVD.Name == vpath) { VirExist = true; break; } } if (VirExist != true) { DirectoryEntry newVirDir = VDir.Children.Add(this.VirPath, "IISWebVirtualDir"); newVirDir.Invoke("AppCreate", true); newVirDir.Properties["AccessRead"][0] = true; newVirDir.Properties["AccessWrite"][0] = true; newVirDir.Properties["AccessExecute"][0] = true; newVirDir.Properties["Accessscript"][0] = true; newVirDir.Properties["ContentIndexed"][0] = true; newVirDir.Properties["AppFriendlyName"][0] = this.VirPath; newVirDir.Properties["Path"][0] = phyPath; VDir.CommitChanges(); newVirDir.CommitChanges(); VDir.Close(); newVirDir.Close(); } } catch (Exception ex) { } } }
|