zoukankan      html  css  js  c++  java
  • C#实现动态发布IIS站点帮助类

    准备工作:

    1、引用 System.DirectoryServices 系统程序集

    2、引用 Microsoft.Web.Administration 程序集,类库位置在 C:WindowsSystem32inetsrvMicrosoft.Web.Administration.dll ,直接拷贝到项目引用即可

    3、调用方式:

    string bing = string.Format("{0}:{1}:{2}", item.BingIp, item.Port, item.BingAddr);
     bool result = IISManager.CreateWebSite(item.SiteName, filePath, bing);

    4、源码:

    public class IISManager
        {
            /// <summary>
            /// 创建一个站点
            /// </summary>
            /// <param name="name">站点名称</param>
            /// <param name="physicalPath">项目所在路径</param>
            /// <param name="bindingInformation">绑定信息</param>
            /// <param name="bindingProtocol">类型,默认http</param>
            /// <returns></returns>
            public static bool CreateWebSite(string name, string physicalPath, string bindingInformation = "*:80:", string bindingProtocol = "http")
            {
                try
                {
                    ServerManager manager = new ServerManager();
                    //判断应用程序池是否存在
                    if (manager.ApplicationPools[name] != null)
                    {
                        manager.ApplicationPools.Remove(manager.ApplicationPools[name]);
                    }
    
                    //判断web应用程序是否存在
                    if (manager.Sites[name] != null)
                    {
                        manager.Sites.Remove(manager.Sites[name]);
                    }
    
                    manager.Sites.Add(name, bindingProtocol, bindingInformation, physicalPath);
    
                    //添加web应用程序池
                    ApplicationPool pool = manager.ApplicationPools.Add(name);
    
                    //设置web应用程序池的Framework版本
                    pool.ManagedRuntimeVersion = "v4.0";
    
                    //设置是否启用32位应用程序
                    pool.SetAttributeValue("enable32BitAppOnWin64", true);
    
                    //设置web网站的应用程序池
                    manager.Sites[name].Applications[0].ApplicationPoolName = name;
    
                    manager.CommitChanges();
    
                    return true;
                }
                catch (Exception e)
                {
                    return false;
                }
            }
    
            /// <summary>
            /// 创建一个站点
            /// </summary>
            /// <param name="name">站点名称</param>
            /// <param name="physicalPath">项目所在路径</param>
            /// <param name="port">端口号</param>
            /// <returns></returns>
            public static bool CreateWebSite(string name, string physicalPath, int port = 80)
            {
                try
                {
                    ServerManager manager = new ServerManager();
                    //判断应用程序池是否存在
                    if (manager.ApplicationPools[name] != null)
                    {
                        manager.ApplicationPools.Remove(manager.ApplicationPools[name]);
                    }
    
                    //判断web应用程序是否存在
                    if (manager.Sites[name] != null)
                    {
                        manager.Sites.Remove(manager.Sites[name]);
                    }
    
                    manager.Sites.Add(name, physicalPath, port);
    
                    //添加web应用程序池
                    ApplicationPool pool = manager.ApplicationPools.Add(name);
    
                    //设置web应用程序池的Framework版本
                    pool.ManagedRuntimeVersion = "v4.0";
    
                    //设置是否启用32位应用程序
                    pool.SetAttributeValue("enable32BitAppOnWin64", true);
    
                    //设置web网站的应用程序池
                    manager.Sites[name].Applications[0].ApplicationPoolName = name;
    
                    manager.CommitChanges();
    
                    return true;
                }
                catch (Exception e)
                {
                    return false;
                }
            }
    
            /// <summary>
            /// 创建虚拟目录
            /// </summary>
            /// <param name="vDirName">虚拟目录名称</param>
            /// <param name="path">实际路径</param>
            /// <param name="iAuth">设置目录的安全性 0-不允许匿名访问,1-为允许,2-基本身份验证,3-允许匿名+基本身份验证,4-整合Windows验证,5-允许匿名+整合Windows验证</param>
            /// <param name="serverName">默认localhost</param>
            /// <returns></returns>
            public static bool CreateVirtualDirectory(string vDirName, string path, int iAuth = 1, string serverName = "localhost")
            {
                try
                {
                    // 确定IIS版本   
                    DirectoryEntry iisSchema = new DirectoryEntry("IIS://" + serverName + "/Schema/AppIsolated");
                    bool iisUnderNt = iisSchema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN";
                    iisSchema.Dispose();
    
                    // 获得管理权限
                    DirectoryEntry iisAdmin = new DirectoryEntry("IIS://" + serverName + "/W3SVC/1/Root");
    
                    // 如果虚拟目录已经存在则删除  
                    foreach (DirectoryEntry v in iisAdmin.Children)
                    {
                        if (v.Name == vDirName)
                        {
                            try
                            {
                                iisAdmin.Invoke("Delete", new object[] { v.SchemaClassName, vDirName });
                                iisAdmin.CommitChanges();
                            }
                            catch (Exception ex)
                            {
                                return false;
                            }
                        }
                    }
    
                    // 创建一个虚拟目录
                    DirectoryEntry vDir = iisAdmin.Children.Add(vDirName, "DefaultWebSiteVirtualDir");
    
                    // 创建一个web应用
                    vDir.Invoke("AppCreate", !iisUnderNt);
    
                    //应用程序名称
                    vDir.Properties["AppFriendlyName"][0] = vDirName;
                    //设置读取权限
                    vDir.Properties["AccessRead"][0] = true;
                    //值 true 表示不论文件类型是什么,文件或文件夹的内容都可以执行
                    vDir.Properties["AccessExecute"][0] = false;
                    //值 true 表示允许用户将文件及其相关属性上载到服务器上已启用的目录中,或者更改可写文件的内容。
                    //只有使用支持 HTTP 1.1 协议标准的 PUT 功能的浏览器,才能执行写入操作
                    vDir.Properties["AccessWrite"][0] = false;
                    //值 true 表示如果是脚本文件或静态内容,则可以执行文件或文件夹的内容。值 false 只允许提供静态文件,如 HTML 文件
                    vDir.Properties["AccessScript"][0] = true;
                    //设置为 true 时,浏览目录时系统会加载该目录的默认文档(由 De, faultDoc 属性指定)
                    vDir.Properties["EnableDefaultDoc"][0] = true;
                    //设置为 true 时,将启用目录浏览
                    vDir.Properties["EnableDirBrowsing"][0] = false;
                    //包含一个或多个默认文档的文件名,如果在客户端的请求中不包含文件名,将把默认文档的文件名返回给客户端
                    vDir.Properties["DefaultDoc"][0] = "login.html,index.html,default.html,Default.aspx,index.aspx";
                    //项目路径
                    vDir.Properties["Path"][0] = path;
                    //作为有效方案返回给客户端的 Windows 验证方案的设置
                    vDir.Properties["AuthFlags"][0] = iAuth;
    
                    // NT格式不支持这特性
                    if (!iisUnderNt)
                    {
                        //页面是否允许当前目录的相对路径(使用 .. 表示法)
                        vDir.Properties["AspEnableParentPaths"][0] = true;
                    }
    
                    // 设置改变
                    vDir.CommitChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
    
        }
     
  • 相关阅读:
    2020-03-23
    2020-03-22
    2020-03-21
    2020-03-20
    2020-03-19
    2020-03-18
    2020-03-17
    单元测试-java
    2020-03-16
    C语言拯救计划Day3-1之求一批整数中出现最多的个位数字
  • 原文地址:https://www.cnblogs.com/dujian123/p/11508138.html
Copyright © 2011-2022 走看看