zoukankan      html  css  js  c++  java
  • C# 创建和删除虚拟目录

            /// <summary>
            /// 创建虚拟目录
            /// </summary>
            /// <param name="WebSite">服务器站点名称</param>
            /// <param name="VDirName">虚拟目录名称</param>
            /// <param name="Path"></param>
            /// <param name="RootDir"></param>
            /// <param name="chkRead"></param>
            /// <param name="chkWrite"></param>
            /// <param name="chkExecute"></param>
            /// <param name="chkScript"></param>
            /// <param name="chkAuth"></param>
            /// <param name="webSiteNum">1</param>
            /// <param name="serverName">localhost</param>
            /// <returns></returns>
            public static bool CreateVDir(string WebSite, string VDirName, string Path, bool RootDir, bool chkRead, bool chkWrite, bool chkExecute, bool chkScript, bool chkAuth, int webSiteNum, string serverName, out string sRet)
            {
                sRet = String.Empty;
                try
                {
                    System.DirectoryServices.DirectoryEntry IISSchema;
                    System.DirectoryServices.DirectoryEntry IISAdmin;
                    System.DirectoryServices.DirectoryEntry VDir;
                    bool IISUnderNT;             // 
                    // 确定IIS版本
                    // 
                    IISSchema = new System.DirectoryServices.DirectoryEntry("IIS://" + serverName + "/Schema/AppIsolated");
                    if (IISSchema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN")
                        IISUnderNT = true;
                    else
                        IISUnderNT = false;
                    IISSchema.Dispose();             // 
                    // Get the admin object 
                    // 获得管理权限
                    // 
                    IISAdmin = new System.DirectoryServices.DirectoryEntry("IIS://" + serverName + "/W3SVC/" + webSiteNum + "/" + WebSite);             // 
                    // If we're not creating a root directory 
                    // 如果我们不能创建一个根目录
                    // 
                    if (!RootDir)
                    {
                        // 
                        // If the virtual directory already exists then delete it 
                        // 如果虚拟目录已经存在则删除
                        foreach (System.DirectoryServices.DirectoryEntry v in IISAdmin.Children)
                        {
                            if (v.Name == VDirName)
                            {
                                // Delete the specified virtual directory if it already exists 
                                //try
                                //{
                                //    IISAdmin.Invoke("Delete", new string[] { v.SchemaClassName, VDirName });
                                //    IISAdmin.CommitChanges();
                                //}
                                //catch (Exception ex)
                                //{
                                //    sRet += ex.Message;
                                //}
                                sRet = "虚拟目录已存在!";
                                return false;
                            }
                        }
                    }
                    // 
                    // Create the virtual directory 
                    // 创建一个虚拟目录
                    // 
                    if (!RootDir)
                    {
                        VDir = IISAdmin.Children.Add(VDirName, "IIsWebVirtualDir");
                    }
                    else
                    {
                        VDir = IISAdmin;
                    }             // 
                    // Setup the VDir 
                    // 安装虚拟目录
                    // 
                    VDir.Properties["AccessRead"][0] = chkRead;
                    VDir.Properties["AccessExecute"][0] = chkExecute;
                    VDir.Properties["AccessWrite"][0] = chkWrite;
                    VDir.Properties["AccessScript"][0] = chkScript;
                    VDir.Properties["AuthNTLM"][0] = chkAuth;
                    VDir.Properties["EnableDefaultDoc"][0] = true;
                    VDir.Properties["EnableDirBrowsing"][0] = false;
                    VDir.Properties["DefaultDoc"][0] = "Default.aspx";
                    VDir.Properties["Path"][0] = Path;             // 
                    // NT doesn't support this property 
                    // NT格式不支持这特性
                    // 
                    if (!IISUnderNT)
                    {
                        VDir.Properties["AspEnableParentPaths"][0] = true;
                    }             // 
                    // Set the changes 
                    // 设置改变
                    // 
                    VDir.CommitChanges();             // 
                    // Make it a web application 
                    // 创建一个web应用
                    // 
                    if (IISUnderNT)
                    {
                        VDir.Invoke("AppCreate", false);
                    }
                    else
                    {
                        VDir.Invoke("AppCreate", 1);
                    }
    
                    return true;
                }
                catch (Exception ex)
                {
                    sRet = ex.Message;
                    return false;
                }
            }
    
            /// <summary>
            /// 判断目录是否存在
            /// </summary>
            /// <param name="serverName">localhost</param>
            /// <param name="vDirName">虚拟目录名称</param>
            /// <param name="webSiteNum">1</param>
            /// <returns></returns>
            public static bool ExistsIIS(string serverName, string vDirName, int webSiteNum)
            {
                bool result = false;
                System.DirectoryServices.DirectoryEntry IISAdmin;
                IISAdmin = new System.DirectoryServices.DirectoryEntry("IIS://" + serverName + "/W3SVC/" + webSiteNum + "/Root");
                foreach (System.DirectoryServices.DirectoryEntry v in IISAdmin.Children)
                {
                    if (v.Name == vDirName)
                    {
                        result = true;
                    }
                }
                return result;
            }
    
            /// <summary>
            /// 删除虚拟目录
            /// </summary>
            /// <param name="serverName">localhost</param>
            /// <param name="vDirName">虚拟目录名称</param>
            /// <param name="webSiteNum">1</param>
            /// <returns></returns>
            public static void DeleteIIS(string serverName, string vDirName, int webSiteNum)
            {
                System.DirectoryServices.DirectoryEntry IISAdmin;
                IISAdmin = new System.DirectoryServices.DirectoryEntry("IIS://" + serverName + "/W3SVC/" + webSiteNum + "/Root");
                foreach (System.DirectoryServices.DirectoryEntry v in IISAdmin.Children)
                {
                    if (v.Name == vDirName)
                    {                    // Delete the specified virtual directory if it already exists 
                        try
                        {
                            IISAdmin.Invoke("Delete", new string[] { v.SchemaClassName, vDirName });
                            IISAdmin.CommitChanges();
                        }
                        catch (Exception ex)
                        {
                            throw;
                        }
    
                    }
                }
            }
  • 相关阅读:
    快速认识 Spring Boot 技术栈
    Spring JDBC Template -实验介绍
    Spring 基于注解的配置 -实验介绍
    Spring 自动扫描与装配 -实验介绍
    Spring IoC 容器 -实验介绍
    Spring 框架的 AOP -实验介绍
    使用 AspectJ 框架实现 Spring AOP
    Spring框架自动创建 proxy
    Spring 框架的 AOP
    考研计算机专业课基础:计算机结构化编程
  • 原文地址:https://www.cnblogs.com/Shadow3627/p/3027257.html
Copyright © 2011-2022 走看看