C#实现动态发布IIS站点帮助类
准备工作:
1、引用 System.DirectoryServices 系统程序集
2、引用 Microsoft.Web.Administration 程序集,类库位置在 C:\Windows\System32\inetsrv\Microsoft.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[].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 = )
{
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[].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 = , 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, "IIsWebVirtualDir"); // 创建一个web应用
vDir.Invoke("AppCreate", !iisUnderNt); //应用程序名称
vDir.Properties["AppFriendlyName"][] = vDirName;
//设置读取权限
vDir.Properties["AccessRead"][] = true;
//值 true 表示不论文件类型是什么,文件或文件夹的内容都可以执行
vDir.Properties["AccessExecute"][] = false;
//值 true 表示允许用户将文件及其相关属性上载到服务器上已启用的目录中,或者更改可写文件的内容。
//只有使用支持 HTTP 1.1 协议标准的 PUT 功能的浏览器,才能执行写入操作
vDir.Properties["AccessWrite"][] = false;
//值 true 表示如果是脚本文件或静态内容,则可以执行文件或文件夹的内容。值 false 只允许提供静态文件,如 HTML 文件
vDir.Properties["AccessScript"][] = true;
//设置为 true 时,浏览目录时系统会加载该目录的默认文档(由 De, faultDoc 属性指定)
vDir.Properties["EnableDefaultDoc"][] = true;
//设置为 true 时,将启用目录浏览
vDir.Properties["EnableDirBrowsing"][] = false;
//包含一个或多个默认文档的文件名,如果在客户端的请求中不包含文件名,将把默认文档的文件名返回给客户端
vDir.Properties["DefaultDoc"][] = "login.html,index.html,default.html,Default.aspx,index.aspx";
//项目路径
vDir.Properties["Path"][] = path;
//作为有效方案返回给客户端的 Windows 验证方案的设置
vDir.Properties["AuthFlags"][] = iAuth; // NT格式不支持这特性
if (!iisUnderNt)
{
//页面是否允许当前目录的相对路径(使用 ..\ 表示法)
vDir.Properties["AspEnableParentPaths"][] = true;
} // 设置改变
vDir.CommitChanges();
return true;
}
catch (Exception ex)
{
return false;
}
} }
DirectoryEntry vDir = iisAdmin.Children.Add(vDirName, "IIsWebVirtualDir");
C#实现动态发布IIS站点帮助类的更多相关文章
- saltstack 发布 iis 站点
Saltstack 发布 iis 站点 saltstack 主服务器配置:切换到 salt 的主目录下 : 主目录示例:/home/salt 程序集放置目录: web/web1 sls 目录: web ...
- 通过代码动态创建IIS站点
对WebApi进行单元测试时,一般需要一个IIS站点,一般的做法,是通过写一个批处理的bat脚本来实现,其实通过编码,也能实现该功能. 主要有关注三点:应用程序池.Web站点.绑定(协议类型:http ...
- .net 程序 动态 控制IIS 站点域名绑定
第一步:引用 导入 System.EnterpriseServices及System.DirectoryServices 两个引用 程序引用: using System.DirectoryServic ...
- 使用批处理自动发布IIS站点,基于IIS7及以上
经过研究,终于使用批处理解决了站点发布步骤多的问题. 完整批处理如下: @set "sitePath=%~dp0" @echo 新建程序池 @C:\Windows\System32 ...
- 今天在发布IIS站点的时候遇到了一些问题
1.HTTP 错误 500.23 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.NET 设置. 分析:一般5XX的错误都是服务器的问题,这里把应用程序池 ...
- 自动发布-asp.net自动发布、IIS站点自动发布(集成SLB、配置管理、Jenkins)
PS:概要.背景.结语都是日常“装X”,可以跳过直接看自动发布 环境:阿里云SLB.阿里云ECS.IIS7.0.Jenkins.Spring.Net 概要 公司一个项目从无到有,不仅仅是系统从无到有的 ...
- [译]MVC网站教程(三):动态布局和站点管理
目录 1. 介绍 2. 软件环境 3. 在运行示例代码之前(源代码 + 示例登陆帐号) 4. 自定义操作结果和控制器扩展 1) OpenFileResult 2) ImageR ...
- Qt 官方一键动态发布技能
苦找了好几天动态库,程序可以运行了,结果没有图标还是少了运行库很苦恼,发现Qt 官方有一键动态发布功能感觉自己萌萌的,来自qt吧亲测可用. 集成开发环境 QtCreator 目前生成图形界面程序 ex ...
- jeecg 3.7.1 新版功能,集群定时任务动态发布模块 使用规则
jeecg 3.7.1 集群定时任务动态发布模块 使用规则 新版特性: 支持集群定时任务,支持分布式. 菜单路径: 系统监控-->定时任务 字段说明: 任务ID.任务说明:自定义即可 ...
随机推荐
- vue报错 :NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDuplicated"}
解决的几种办法 https://blog.csdn.net/weixin_43202608/article/details/98884620 这个适合所有vue的UI框架 在main.js下添加一下代 ...
- easy-table-vue+VueJs、SpringBoot+Mybatis实现MVVM模型前后台数据交互
该项目分为前端展示部分和后台服务部分. 前端部分 使用的技术是:NodeJs.Webpack.VueJs 使用的组件库是:IVIEW.easy-table-vue 使用的开发工具是:WebStorm ...
- P1026 统计单词个数——substr
P1026 统计单词个数 string 基本操作: substr(x,y) x是起始位置,y是长度: 返回的是这一段字符串: 先预处理sum[i][j],表示以i开头,最多的单词数: 从后往前寻找,保 ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2)
A - Forgetting Things 题意:给 \(a,b\) 两个数字的开头数字(1~9),求使得等式 \(a=b-1\) 成立的一组 \(a,b\) ,无解输出-1. 题解:很显然只有 \( ...
- ansible handlers
示例:安装nginx --- - hosts: hadoop #指定主机组 remote_user: root #远程执行命令的用户 gather_facts: no #是否获取远程主机的信息 tas ...
- vue的ui组件库
https://www.cnblogs.com/dupd/p/7735450.html
- 深入理解JVM虚拟机3:垃圾回收器详解
JVM GC基本原理与GC算法 Java的内存分配与回收全部由JVM垃圾回收进程自动完成.与C语言不同,Java开发者不需要自己编写代码实现垃圾回收.这是Java深受大家欢迎的众多特性之一,能够帮助程 ...
- GC和GC分配策略
一.内存如何回收 解决如何回收问题,首先需要解决回收对象的问题?什么样的对象需要回收,怎么样的不需要回收?保证有引用的内存不被释放:回收没有指针引用的内存是Collector的职责,在保证没有指针引用 ...
- 异步任务神器 和定时任务Celery
异步任务神器 Celery Celery 在程序的运行过程中,我们经常会碰到一些耗时耗资源的操作,为了避免它们阻塞主程序的运行,我们经常会采用多线程或异步任务.比如,在 Web 开发中,对新用户的注册 ...
- ML_Review_PCA(Ch4)
Note sth about PCA(Principal Component Analysis) ML6月20日就要考试了,准备日更博客,来记录复习一下这次ML课所学习的一些方法. 博客是在参考老 ...
