使用ASP.NET創建IIS站點或虛擬目錄(下)


/// <summary>
        /// Delete the virtual directory in the WebServer
        ///</summary>
        ///<param name="website">webserver name</param>
        ///<param name="vdir">virtual directory name</param>
        public static void DeleteVirtualDir(string website, string vdir)
        {
            if (!GetVirtualDir(website, vdir)) throw new Exception(" The virtual directory don't exist in the website");
            using (DirectoryEntry de = GetWebSiteInfo(website))
            {
                foreach (DirectoryEntry sub in de.Children)
                {
                    if (sub.Name == vdir)
                    {
                        de.Invoke("Delete", new string[] { sub.SchemaClassName, vdir });
                        de.CommitChanges();
                    }
                }
            }
        }
 
 
        private static void UpdateVDirInfo(DirectoryEntry newDE, ref System.Data.PropertyCollection properties)
        {
            //newDE.Properties["AnonyMousUserName"][0] = properties["AnonymousUserName"].ToString();
            //newDE.Properties["AnonymousUserPass"][0] = properties["AnonymousUserPass"].ToString();
            newDE.Properties["AccessRead"][0] = (bool)properties["AccessRead"];
            newDE.Properties["AccessExecute"][0] = (bool)properties["AccessExecute"];
            newDE.Properties["AuthBasic"][0] = (bool)properties["AuthBasic"];
            newDE.Properties["AuthNTLM"][0] = (bool)properties["AuthNTLM"];
            newDE.Properties["ContentIndexed"][0] = (bool)properties["ContentIndexed"];
            newDE.Properties["EnableDefaultDoc"][0] = (bool)properties["EnableDefaultDoc"];
            newDE.Properties["EnableDirBrowsing"][0] = (bool)properties["EnableDirBrowsing"];
            newDE.Properties["AccessSSL"][0] = (bool)properties["AccessSSL"];
            newDE.Properties["AccessScript"][0] = (bool)properties["AccessScript"];
            newDE.Properties["DefaultDoc"][0] = properties["DefaultDoc"].ToString();
            newDE.Properties["Path"][0] = properties["Path"].ToString();
            newDE.Properties["AppIsolated"][0] = (int)properties["AppIsolated"];
            newDE.Properties["AppFriendlyName"][0] = properties["AppFriendlyName"].ToString();
            newDE.Properties["AccessFlags"][0] = (int)properties["AccessFlags"];
            newDE.Properties["FrontPageWeb"][0] = (int)properties["FrontPageWeb"];
            //newDE.Properties["DontLog"][0] = (bool)properties["DontLog"];
            //newDE.Properties["AppRoot"][0] = properties["AppRoot"].ToString();
        }
 
 
 
        private static bool GetVirtualDir(string webSite, string dirName)
        {
            bool result = false;
            using (DirectoryEntry de = GetWebSiteInfo(webSite))
            {
               
                if (de != null)
                {
                    foreach (DirectoryEntry subVD in de.Children)
                    {
                        if (subVD.SchemaClassName == "IIsWebVirtualDir" && subVD.Name == dirName)
                        {
                            result = true;
                            break;
                        }
                    }
                }
            }
            return result;
           
        }
 
        private static void GetWebSiteInfo(ref Hashtable webServer)
        {
            DirectoryEntries des = iisDE.Children;
            foreach (DirectoryEntry subDE in des)
            {
               if (subDE.SchemaClassName == "IIsWebServer")
               {
                  webServer.Add(subDE.Properties["ServerComment"].Value.ToString(), subDE.Name);
               }
            }
            des = null;
      
        }
 
        private static DirectoryEntry GetWebSiteInfo(string website)
        {
            DirectoryEntry result = null;
            DirectoryEntries des = iisDE.Children;
            foreach (DirectoryEntry subDE in des)
            {
                if (subDE.SchemaClassName == "IIsWebServer" && subDE.Properties["ServerComment"].Value.ToString() == website)
                {
                    result = subDE.Children.Find("Root", "IIsWebVirtualDir");
                    break;
                }
 
            }
            des = null;
            return result;
 
        }
 
        private static int GetWebSiteInfo(int port)
        {
            int result = 1,i=1;
            DirectoryEntries des = iisDE.Children;
            foreach (DirectoryEntry subDE in des)
            {
                if (subDE.SchemaClassName == "IIsWebServer")
                {
                    if ((i = Convert.ToInt32(subDE.Name)) >= result)
                    {
                        result = i + 1;
                    }
                    if (subDE.Properties["ServerBindings"][0].ToString() == ":" + port.ToString() + ":")
                    {
                        throw new Exception(" The port is already used");
                    }
                }
           }
           des = null;
           return result;
        }
 
               
}
 
 
注意在很多文章中認為vde.Invoke("AppCreate", true);可有可無,但該方法能激活網站應用,相當於在一個虛擬目錄中的Application settings中點擊Create按鈕來創建該目錄的應用

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com