LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

【C#】ServerManager更改IIS7设置

admin
2021年5月11日 16:18 本文热度 4333

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Web.Administration;

namespace IIS自动安装程序_控制台版v1._0_20100727_zch
{
    public class IIS7Core1
    {
        /// <summary>
        /// 创建站点
        /// </summary>
        /// <param name="siteName">站点名称</param>
        /// <param name="bindingInfo">绑定<example>"*:80:myhost.com"</example></param>
        /// <param name="physicalPath">物理路径</param>
        /// <param name="pathToResourceFile">资源路径</param>
        /// <param name="pathTo公用上传">资源路径</param>
        /// <param name="password">FileUser密码</param>
        /// <param name="defaultDocName">默认首页</param>
        public static void Create(string siteName, string bindingInfo, string physicalPath, string pathToResourceFile, string pathTo公用上传, string password, string defaultDocName)
        {
            //Delete an existent web site.
            ServerManager iisManager = new ServerManager();
            Site site = iisManager.Sites[siteName];
            if (site != null)
            {
                iisManager.Sites.Remove(site);
                iisManager.CommitChanges();
            }

            //Delete an existent web site app pool.
            iisManager = new ServerManager();
            ApplicationPool oldpool = iisManager.ApplicationPools[siteName + "Pool"];
            if (oldpool != null)
            {
                iisManager.ApplicationPools.Remove(oldpool);
                iisManager.CommitChanges();
            }

            iisManager = new ServerManager();
            //创建站点
            iisManager.Sites.Add(siteName, "http", bindingInfo, physicalPath);
            if (true)
            {
                //创建应用程序池
                ApplicationPool pool = iisManager.ApplicationPools.Add(siteName + "Pool");

                //设置应用程序池标识
                pool.ProcessModel.IdentityType = ProcessModelIdentityType.ApplicationPoolIdentity;

                //if (!String.IsNullOrEmpty(appPoolUserName))
                //{
                //    pool.ProcessModel.UserName = appPoolUserName;
                //    pool.ProcessModel.Password = appPoolPassWord;
                //}

                //设置应用程序池的托管管道模式
                pool.ManagedPipelineMode = ManagedPipelineMode.Classic;

                //设置应用程序池的.net版本
                pool.ManagedRuntimeVersion = "v4.0";

                //设置应用程序池的名称
                iisManager.Sites[siteName].Applications["/"].ApplicationPoolName = pool.Name;
            }
            //将一个应用程序(Application)添加到一个站点
            //iisManager.Sites[siteName].Applications.Add("/" + siteName, physicalPath);

            //建立虚拟目录(Virtual Directory)
            Application app = iisManager.Sites[siteName].Applications[0];

            if (siteName.ToLower().Contains("web"))
            {
                //虚拟目录
                app.VirtualDirectories.Add("/ResourceFile", pathToResourceFile);
                app.VirtualDirectories.Add("/UploadCenter", pathTo公用上传);
                app.VirtualDirectories.Add("/UploadFile", pathTo公用上传);

                //设置每个虚拟目录的认证
                app.VirtualDirectories["/ResourceFile"].UserName = "FileUser";
                app.VirtualDirectories["/ResourceFile"].Password = password;
                app.VirtualDirectories["/UploadCenter"].UserName = "FileUser";
                app.VirtualDirectories["/UploadCenter"].Password = password;
                app.VirtualDirectories["/UploadFile"].UserName = "FileUser";
                app.VirtualDirectories["/UploadFile"].Password = password;

            }
            else if (siteName.ToLower().Contains("sdzn"))
            {
                app.VirtualDirectories.Add("/Upload", pathTo公用上传);
                app.VirtualDirectories["/Upload"].UserName = "FileUser";
                app.VirtualDirectories["/Upload"].Password = password;
            }

            else//wcf
            {
                app.VirtualDirectories.Add("/ResourceFile", pathToResourceFile);
                app.VirtualDirectories.Add("/UploadFile", pathTo公用上传);
                app.VirtualDirectories["/ResourceFile"].UserName = "FileUser";
                app.VirtualDirectories["/ResourceFile"].Password = password;
                app.VirtualDirectories["/UploadFile"].UserName = "FileUser";
                app.VirtualDirectories["/UploadFile"].Password = password;
            }

            iisManager.CommitChanges();

            //在站点上添加默认文档
            iisManager = new ServerManager();
            Configuration cfg = iisManager.GetWebConfiguration(siteName);
            ConfigurationSection defaultDocumentSection = cfg.GetSection("system.webServer/defaultDocument");
            ConfigurationElement filesElement = defaultDocumentSection.GetChildElement("files");
            ConfigurationElementCollection filesCollection = filesElement.GetCollection();

            foreach (ConfigurationElement elt in filesCollection)
            {
                if (elt.Attributes["value"].Value.ToString() == defaultDocName)
                {
                    return;
                }
            }

            try
            {
                ConfigurationElement docElement = filesCollection.CreateElement();
                docElement.SetAttributeValue("value", defaultDocName);
                filesCollection.Add(docElement);
            }
            catch (Exception) { }   //this will fail if existing

            iisManager.CommitChanges();
        }
    }
}


该文章在 2021/5/11 16:18:22 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved