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.任务说明:自定义即可 ...
随机推荐
- 使用jQuery快速高效制作网页交互特效--JavaScript操作BOM对象
JavaScript操作BOM 一.window对象: 二.window对象的属性和方法 1.windows对象的常用属性: 语法:window.属性名="属性值" 2.windo ...
- B5G/6G新技术
组网技术:由自组织向自支撑发展:卫星通信(大尺度衰落)采用DTN组网. 多址技术:非正交多址:Polar-SCMA:交织多址:IDMA. 信道技术:多径分集.多普勒分集.OFDM的CP用ZP替代.设计 ...
- sql 存储过程set nocount on 的作用
在存储过程中,经常用到SET NOCOUNT ON: 作用:阻止在结果集中返回显示受T-SQL语句或则usp影响的行计数信息.当SET ONCOUNT ON时候,不返回计数,当SET NOCOUNT ...
- Peaks 线段树合并
Peaks 线段树合并 \(n\)个带权值\(h_i\)山峰,有\(m\)条山峰间双向道路,\(q\)组询问,问从\(v_i\)开始只经过\(h_i\le x\)的路径所能到达的山峰中第\(k\)高的 ...
- 5.Python3列表和元组
5.1序列 在python3中序列结构主要有列表.元组.集合.字典和字符串,对于这些序列有以下通用操作. 5.1.1 索引 序列中的每一个元素都有 一个编号,也称为索引.这个索引是从0开始递增的,即下 ...
- mysqlslap压力测试时出现"Can't connect to MySQL server"
mysqlslap -utest -h 192.168.1.12 -p'test' --concurrency=100 --iterations=500 --create-schema='my_db' ...
- [线性代数] 线性代数入门A Gentle Introduction
An Overview: System of Linear Equations Basically, linear algebra solves system of linear equations ...
- elasticsearch bootstrap.memory_lock
检查bootstrap.memory_lock设置是否生效 get http://10.127.0.1:9200/_nodes?filter_path=**.mlockall 响应: { " ...
- cannot load from mysql.proc. the table is probably corrupted 解决办法
执行以下命令:mysql_upgrade -u root -p 密码 mysql5.5及5.5以上的版本开始,mysql数据库中proc表中的comment字段的列属性已经由char(64)改为tex ...
- arcgis python 布局中所有元素平移
# Author: ESRI # Date: July 5, 2010 # Version: ArcGIS 10.0 # Purpose: This script will loop through ...
