.net操作IIS,新建网站,新建应用程序池,设置应用程序池版本,设置网站和应用程序池的关联
ServerManager类用来操作IIS,提供了很多操作IIS的API。使用ServerManager必须引用Microsoft.Web.Administration.dll,具体路径为:%windir%\System32\Inetsrv\Microsoft.Web.Administration.dll
以下代码展示了如何将自己的ASP.NET网站自动部署到IIS的过程,其中包括,新建网站-》新建应用程序池-》设置网站和应用程序池的关联-》重启网站和应用程序池
具体代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Web.Administration; namespace AutoConfigureIIS
{
internal class Program
{
//don't forget to add a reference to %windir%\System32\Inetsrv\Microsoft.Web.Administration.dll private static ServerManager serverMgr = new ServerManager();
private static string applicationPoolName = "IPSRemoteApplicationPool";
private static string applicationPoolVersion = "v4.0";
private static string websiteName = "IPSRemoteSite";
private static string port = "";
private static string webPath = @"C:\Work\Published\SelfHostedIPSRemoteWeb"; private static void Main(string[] args)
{
DeployIPSWeb(); Console.Read();
} /// <summary>
/// Auto Configure IPS WebSite to IIS
/// </summary>
private static void DeployIPSWeb()
{
//create Application Pool
if (!IsApplicationPoolExists(applicationPoolName))
{
CreateApplicationPool(applicationPoolName);
}
//create website
if (!IsWebsiteExists(websiteName))
{
CreateWebSite(websiteName, port, webPath);
} Site site = FindSiteBySiteName(websiteName); // get site by Index or by siteName
ApplicationPool appPool = FindApplicationPoolByName(applicationPoolName);
// get appPool by Index or by appPoolName site.Stop();
site.ApplicationDefaults.ApplicationPoolName = appPool.Name; foreach (var item in site.Applications)
{
item.ApplicationPoolName = appPool.Name;
} serverMgr.CommitChanges(); // this one is crucial!!! see MSDN:
// Updates made to configuration objects must be explicitly written to the configuration
// system by using the CommitChanges method!!
site.Start();
appPool.Recycle();
} /// <summary>
/// Create WebSite sitename to IIS with the arguments
/// </summary>
/// <param name="sitename"></param>
/// <param name="portstr"></param>
/// <param name="webPathstr">website code path</param>
public static void CreateWebSite(string sitename, string portstr, string webPathstr)
{
serverMgr.Sites.Add(sitename, "http", "*:" + portstr + ":", webPathstr); serverMgr.CommitChanges();
} public static void CreateApplicationPool(string poolname)
{
ApplicationPool newPool = serverMgr.ApplicationPools.Add(poolname);
newPool.ManagedRuntimeVersion = applicationPoolVersion;
serverMgr.CommitChanges();
} public static bool IsWebsiteExists(string strWebsitename)
{
Site site = FindSiteBySiteName(strWebsitename);
if (site == null)
return false;
else
return true;
} public static Site FindSiteBySiteName(string strWebsitename)
{
SiteCollection sitecollection = serverMgr.Sites;
foreach (Site site in sitecollection)
{
if (site.Name == strWebsitename.ToString())
{
return site;
}
}
return null;
} public static ApplicationPool FindApplicationPoolByName(string poolName)
{
ApplicationPoolCollection poolcollection = serverMgr.ApplicationPools;
foreach (ApplicationPool pool in poolcollection)
{
if (pool.Name == poolName.ToString())
{
return pool;
}
}
return null;
} public static bool IsApplicationPoolExists(string appPool)
{
ApplicationPool pool = FindApplicationPoolByName(appPool);
if (pool == null)
{
return false;
}
else
{
return true;
}
}
}
}
.net操作IIS,新建网站,新建应用程序池,设置应用程序池版本,设置网站和应用程序池的关联的更多相关文章
- 利用ASP.NET操作IIS (可以制作安装程序)
很多web安装程序都会在IIS里添加应用程序或者应用程序池,早期用ASP.NET操作IIS非常困难,不过,从7.0开始,微软提供了 Microsoft.Web.Administration 类,可以很 ...
- C#操作IIS程序池及站点的创建配置
最近在做一个WEB程序的安装包:对一些操作IIS进行一个简单的总结:主要包括对IIS进行站点的新建以及新建站点的NET版本的选择,还有针对IIS7程序池的托管模式以及版本的操作:首先要对Microso ...
- C#操作IIS程序池及站点的创建配置(转)
原文:http://www.cnblogs.com/wujy/archive/2013/02/28/2937667.html 最近在做一个WEB程序的安装包:对一些操作IIS进行一个简单的总结:主 ...
- C#操作IIS程序池及站点的创建配置实现代码
首先要对Microsoft.Web.Administration进行引用,它主要是用来操作IIS7: using System.DirectoryServices;using Microsoft.We ...
- C#使用DirectoryEntry操作IIS创建网站和虚拟路径
原文:http://www.cnblogs.com/Aiooioo/archive/2011/05/30/cs-iis.html 在.Net中我们可以使用内置的类DirectoryEntry来承载II ...
- C#操作IIS完整解析
原文:C#操作IIS完整解析 最近在为公司实施做了一个工具,Silverlight部署早已是轻车熟路, 但对于非技术人员来说却很是头疼的一件事,当到现场实施碰到客户情况也各不相同, 急需一个类似系统备 ...
- C#操作IIS服务
进入正题:先从使用角度来讲解IIS操作,然后再深入到具体的IIS服务底层原理. [1]前提掌握要点: (1).IIS到目前经历了四个版本分别为 IIS4.0 IIS5.0 IIS6.0 IIS7.0, ...
- c#操作IIS站点
/// <summary> /// 获取本地IIS版本 /// </summary> /// <returns></returns> public st ...
- C# 使用代码来操作 IIS
由于需要维护网站的时候,可以自动将所有的站点HTTP重定向到指定的静态页面上. 要操作 IIS 主要使用到的是“Microsoft.Web.Administration.dll”. 该类库不可以在引用 ...
随机推荐
- Linux常用操作命令(二)
ab命令压测: ab -n 1 -c 1 -p post.txt -T 'application/x-www-form-urlencoded' -H 'User-U:2Lh72GM2UumEAnZzM ...
- vue1与vue2的路由 以及vue2项目大概了解
vue1的路由 1.设置根组件 Vue.extend() 2.设置局部组件 Vue.extend({template:"/home"}) 3.实例化路由 var route ...
- Python3分析sitemap.xml抓取导出全站链接
最近网站从HTTPS转为HTTP,更换了网址,旧网址做了301重定向,折腾有点大,于是在百度站长平台提交网址,不管是主动推送还是手动提交,前提都是要整理网站的链接,手动添加太麻烦,效率低,于是就想写个 ...
- htm5
htm5在html4.0. xhtm1.0的基础上增加了音频.视频.拖拽等功能,不过,htmL5,还在完善中,不过大部分浏览器都已经支持了部分功能. 兼容性: 最新版本的 Safari.Chrome. ...
- C#注册表操作类--完整优化版
using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; namespace ...
- scrollMenu,一款可滚动的菜单插件 兼容pc和移动端
这个菜单 有两种样式 , 也可以通过animate.css加不同的动画效果!滚动的方式也有两种 一种为通用的overflow,另外一种是better-scroll的滚动效果 在线链接地址 ht ...
- tcp/ip通信传输流
利用TCP/IP协议族进行网络通信时,会通过分层顺序与对方进行通信,发送端从应用层往下走,接收端则往应用层方向走. 我们用HTTP进行举例 客户端在应用层发出想要看到某个web页面的http请求.HT ...
- Echarts展示百分比的问题
22.echarts 想要自定义tooltip 的百分比的时候,可以在formatter中console.log(params); 当鼠标移动到y轴的时候会触发输出;
- 在线用户数-Constants
package com.pb.news.constants; public class Constants { public static int ONLINE_USER_COUNT=0;//在线用户 ...
- 英文版windows7中文软件显示乱码的解决办法
一.打开控制面板,修改语言的归属地为China 修改完成之后重启,一般能解决大部分问题,如果依然有部分显示乱码,就需要去修改注册表