zoukankan      html  css  js  c++  java
  • 使用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按钮来创建该目录的应用
  • 相关阅读:
    在Ubuntu 20.04 LTS Focal Fossa上安装Deluge
    如何使用命令行快速检查Linux系统的版本
    如何在Debian 10上安装NVM
    如何在CentOS 8上安装Apache ActiveMQ
    如何在Linux中引导时列出启动服务?
    如何检查Linux Mint 20磁盘错误的方法
    如何在Firewalld中打开特定IP地址的端口?
    如何在CentOS 8服务器安装oVirt开源虚拟化管理系统
    如何在Centos 8服务器上安装LogAnalyzer?
    如何在Debian中使用apt从命令行安装程序
  • 原文地址:https://www.cnblogs.com/scgw/p/1944606.html
Copyright © 2011-2022 走看看