C#管理IIS中的站点
原文:http://www.knowsky.com/534237.html
Microsoft自Windows Vista一起发布了IIS 7.0,这个已经是去年的话题了,随后,由.NET开发的Web程序便逐步从IIS 6.0过渡到IIS 7.0上了。IIS 7.0提供了很多比上一版本更多的新特性,包括完全模块化的组件、文本文件的配置功能、MMC图形模式管理工具等等,并且与.NET编程语言结合得更加紧密了,在新添加的Microsoft.Web.Administration名称空间中也增加了很多用于管理和访问IIS的对象,从而使得通过编程方式操作IIS更加简便。虽然在IIS 6.0时代我们也可以非常轻松地通过C#来管理服务器的IIS,但相对来说,现在需要编写的代码更少,所能完成的功能更强。以下是我在曾经做的一个项目中所写的一个类库中的一部分,主要实现了对IIS 7.0的操作,包括创建和删除站点、创建和删除虚拟目录、创建和删除应用程序池、添加站点默认文档、判断站点和虚拟目录是否存在、以及检查Bindings信息等。
对于IIS 7.0的介绍读者如果有兴趣的话可以看看下面的两篇文章,我觉得不错!
http://blog.joycode.com/scottgu/archive/2007/04/08/100650.aspx
http://msdn.microsoft.com/en-us/magazine/cc163453.aspx
不说废话了,赶紧贴代码吧。
首先是对站点的管理。我写了一个相对较为通用的私有方法,然后在对外的方法中给出了调用接口,包括了创建站点时应用程序池的创建和权限的管理。
CreateSite
/// <summary>
/// Create a new web site.
/// </summary>
/// <param name="siteName"></param>
/// <param name="bindingInfo">"*:<port>:<hostname>" <example>"*:80:myhost.com"</example></param>
/// <param name="physicalPath"></param>
public static void CreateSite(string siteName, string bindingInfo, string physicalPath)
{
createSite(siteName, "http", bindingInfo, physicalPath, true, siteName + "Pool",PRocessModelIdentityType.NetworkService, null, null, ManagedPipelineMode.Integrated, null);
}
private static void createSite(string siteName, string protocol, string bindingInformation, string physicalPath,
bool createAppPool, string appPoolName, ProcessModelIdentityType identityType,
string appPoolUserName, string appPoolPassWord, ManagedPipelineMode appPoolPipelineMode, string managedRuntimeVersion)
{
using (ServerManager mgr = new ServerManager())
{
Site site = mgr.Sites.Add(siteName, protocol, bindingInformation, physicalPath);
// PROVISION APPPOOL IF NEEDED
if (createAppPool)
{
applicationPool pool = mgr.ApplicationPools.Add(appPoolName);
if (pool.ProcessModel.IdentityType != identityType)
{
pool.ProcessModel.IdentityType = identityType;
}
if (!String.IsNullOrEmpty(appPoolUserName))
{
pool.ProcessModel.UserName = appPoolUserName;
pool.ProcessModel.Password = appPoolPassword;
}
if (appPoolPipelineMode != pool.ManagedPipelineMode)
{
pool.ManagedPipelineMode = appPoolPipelineMode;
}
site.Applications["/"].ApplicationPoolName = pool.Name;
}
mgr.CommitChanges();
}
}
这个是删除站点的方法,比较简单。
DeleteSite
/// <summary>
/// Delete an existent web site.
/// </summary>
/// <param name="siteName">Site name.</param>
public static void DeleteSite(string siteName)
{
using (ServerManager mgr = new ServerManager())
{
Site site = mgr.Sites[siteName];
if (site != null)
{
mgr.Sites.Remove(site);
mgr.CommitChanges();
}
}
}
然后是对虚拟目录的操作,包括创建和删除虚拟目录,都比较简单。
CreateVDir
public static void CreateVDir(string siteName, string vDirName, string physicalPath)
{
using (ServerManager mgr = new ServerManager())
{
Site site = mgr.Sites[siteName];
if (site == null)
{
throw new ApplicationException(String.Format("Web site {0} does not exist", siteName));
}
site.Applications.Add("/" + vDirName, physicalPath);
mgr.CommitChanges();
}
}
DeleteVDir
public static void DeleteVDir(string siteName, string vDirName)
{
using (ServerManager mgr = new ServerManager())
{
Site site = mgr.Sites[siteName];
if (site != null)
{
Microsoft.Web.Administration.Application app = site.Applications["/" + vDirName];
if (app != null)
{
site.Applications.Remove(app);
mgr.CommitChanges();
}
}
}
}
删除应用程序池。
DeletePool
/// <summary>
/// Delete an existent web site app pool.
/// </summary>
/// <param name="appPoolName">App pool name for deletion.</param>
public static void DeletePool(string appPoolName)
{
using (ServerManager mgr = new ServerManager())
{
ApplicationPool pool = mgr.ApplicationPools[appPoolName];
if (pool != null)
{
mgr.ApplicationPools.Remove(pool);
mgr.CommitChanges();
}
}
}
在站点上添加默认文档。
AddDefaultDocument
public static void AddDefaultDocument(string siteName, string defaultDocName)
{
using (ServerManager mgr = new ServerManager())
{
Configuration cfg = mgr.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
mgr.CommitChanges();
}
}
检查虚拟目录是否存在。
VerifyVirtualPathIsExist
public static bool VerifyVirtualPathIsExist(string siteName, string path)
{
using (ServerManager mgr = new ServerManager())
{
Site site = mgr.Sites[siteName];
if (site != null)
{
foreach (Microsoft.Web.Administration.Application app in site.Applications)
{
if (app.Path.ToUpper().Equals(path.ToUpper()))
{
return true;
}
}
}
}
return false;
}
检查站点是否存在。
VerifyWebSiteIsExist
public static bool VerifyWebSiteIsExist(string siteName)
{
using (ServerManager mgr = new ServerManager())
{
for (int i = 0; i < mgr.Sites.Count; i++)
{
if (mgr.Sites[i].Name.ToUpper().Equals(siteName.ToUpper()))
{
return true;
}
}
}
return false;
}
检查Bindings信息。
VerifyWebSiteBindingsIsExist
public static bool VerifyWebSiteBindingsIsExist(string bindingInfo)
{
string temp = string.Empty;
using (ServerManager mgr = new ServerManager())
{
for (int i = 0; i < mgr.Sites.Count; i++)
{
foreach (Microsoft.Web.Administration.Binding b in mgr.Sites[i].Bindings)
{
temp = b.BindingInformation;
if (temp.IndexOf('*') < 0)
{
temp = "*" + temp;
}
if (temp.Equals(bindingInfo))
{
return true;
}
}
}
}
return false;
}
以上代码均在Windows Vista SP1和Windows Server 2008上测试通过,使用时需要在工程中引用Microsoft.Web.Administration类库,该类库为IIS 7.0自带的。
C#管理IIS中的站点的更多相关文章
- 用《内网穿山甲》把本地IIS中的站点共享到远程访问
前言: 因为各种原因,我们常常要把本机或局域网中搭建的站点发给远方的人访问,他有可能是测试人员.客户.前端.或领导演示,或是内部系统内部论坛临时需要在远程访问,事件变得很麻烦,要么有公网IP,要么能控 ...
- PowerShell管理IIS(新建站点、应用程序池、应用程序、虚拟目录等)
#导入IIS管理模块 Import-Module WebAdministration #新建应用程序池 api.dd.com New-Item iis:\AppPools\api.dd.com Set ...
- django中的站点管理
所谓网页开发是有趣的,管理界面是千篇一律的.所以就有了django自动管理界面来减少重复劳动. 一.激活管理界面 1.django.contrib包 django自带了很多优秀的附加组件,它们都存在于 ...
- HTML5游戏开发框架phaser学习日志(一)下载phaser,在IIS中配置phaser的examples站点
phaser是HTML5开源的游戏引擎. 一.源码下载地址:https://github.com/photonstorm/phaser 二.文档结构: 三.将phaser-master部署到IIS中站 ...
- 从本机IIS中管理 远程服务器 IIS
有时候,一般情况下,我们对服务器上 IIS 上的管理局限于 使用远程桌面:现在介绍一种,通过 本机 管理管理远程IIS 的方法! 1. 服务器端设置: 服务器管理器 ==>增加角色和功能向导= ...
- IIS中添加ftp站点
1.创建Windows账号 右击点击“我的电脑”,选择“管理”打开服务器管理的控制台.展开“服务器管理器”,一路展开“配置”.“本地用户和组”,点“用户”项.然后在右边空白处点右键,选择“新用户”将打 ...
- IIS中配置WCF站点
http://msdn.microsoft.com/zh-cn/library/aa751852.aspx http://blog.csdn.net/hsg77/article/details/389 ...
- 关于 IIS 中 Excel 访问的问题
关于 IIS 上 Excel 文件的访问, 一路上困难重重, 最后按以下步骤进行设置, 可在 IIS 中正常使用! 1. 引用及代码: 1). 项目中添加 Excel 程序集引用(注意: 从系统 CO ...
- 【续集】在 IIS 中部署 ASP.NET 5 应用程序遭遇的问题
dudu 的一篇博文:在 IIS 中部署 ASP.NET 5 应用程序遭遇的问题 针对 IIS 部署 ASP.NET 5 应用程序的问题,在上面博文中主要采用两种方式尝试: VS2015 的 Publ ...
随机推荐
- 实施双工通信框架:SignalR
SignalR:基于Asp.net平台构建,利用JavaScript或者Websockets,实现在客户端与服务端异步通信的框架. Html5新规范:WebSocket
- 后台js
Response.Write("<script>alert('该用户名不存在或密码错误或未参加教学活动,请重新输入!');history.back()</script> ...
- Linux下oracle 11g安装
服务器环境要求 硬盘 20G以上 ,必须高于1G的物理内存,交换空间一般为内存的2倍,例如:1G的内存可以设置swap 分区为3G大小 在Root用户下执行以下步骤: 修改用户的SHELL ...
- 创建存储过程和函数【weber出品必属精品】
一.什么是存储过程和函数 1. 是被命名的pl/sql块 2. 被称之为pl/sql子程序 3. 与匿名块类似,有块结构: 声明部分是可选的(没有declare关键字) 必须有执行部分 可选的异常处理 ...
- ios面试汇总
http://www.360doc.com/content/15/0707/01/26281448_483232245.shtml
- iOS开发-清理缓存功能的实现
移动应用在处理网络资源时,一般都会做离线缓存处理,其中以图片缓存最为典型,其中很流行的离线缓存框架为SDWebImage. 但是,离线缓存会占用手机存储空间,所以缓存清理功能基本成为资讯.购物.阅读类 ...
- [Mac] 使用Mac时的一些技巧
这篇博客就用来记录自己在使用Mac时学来的一些技巧吧! 1. 如何开启 Sticky key (在屏幕上显示输入的控制键) 就是这个东西啦,就是在视频演示的时候让别人看到自己按了什么控制键. 在s ...
- myeclipse10 如何把代码预览的窗口去掉
1,选择菜单: windows -> preferences2,在弹出窗口中选择General-> Editors -> FileAssociations3,在上方框内选择*.jsp ...
- 网页title标题的闪动效果
通过网页title来提示用户有新消息这个功能很常见,比如现在的微博,还有一些邮箱,这个功能都很常见. 显示信息数: <input type="text" id="t ...
- PHP & JAVA 实现 PBKDF2 加密算法
PHP代码: /** * PBKDF2 加密函数 * 参考标准 * @link https://www.ietf.org/rfc/rfc2898.txt * * php官方函数将在php5.5发布 * ...