January 23,2008

IIS config

今天就來小記一下學習的心得吧!! 
 .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) {            }
           

        }
}



Posted by adrawgill at 樂多Roodo! │23:58 │回應(1)引用(0)
樂多分類:工作/職場 工具:加入樂多書籤編輯本文
Ads by Roodo! 

引用URL

http://cgi.blog.roodo.com/trackback/5071031
回應文章

謝謝您來看我
祝您新年快樂
\ ▏/
   _/\_
   \__/

       ┌──────┐
      ┌┴──────┴┐
  ████│  財 神 到 ∣████
      └────────┘
     ╭│ ▃▃  ▃▃ │╮
     ││ ⊙    ⊙ ∣∣
     ╰│ ○ ╰╯ ○ │╯
  █────────────────█
 ∩█      恭祝大家      █∩
(  )     新年快樂唷     (  )
  █  好運連連 財源滾滾來!   █
  █                █
  █   天天快樂 年年如意    █
  █─┬────────────┬─█
    │▔▔▔▔▔▔▔▔▔▔▔▔│
    ﹨            ∕
    │            │
    ∕/\________/\﹨
    ◢███◣    ◢███◣
    █████    █████

此先給你拜個年!
願你過往的陰霾,
在新年的這一刻起全消失殆盡,
來的是福氣、好運、財源滾滾來!
Posted by 小靜 at February 9,2008 13:31