.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”. 该类库不可以在引用 ...
随机推荐
- java数据类型转换那点事
public class kkk { /** * 先看看eclipse对于数值型转换会有哪些报错,但是有一点必须明确,eclipse不报错的,不一定就是说这种思维逻辑是对的 * 可以直接将代码复制过去 ...
- Lamp单独安装(windows下)
安装的软件清单:apache_2.2.9-win32-x86-openssl-0.9.8h-r2.msimysql-5.1.28-rc-win32.zipphp-5.2.6-Win32.zipphpM ...
- 【LeetCode】122. Best Time to Buy and Sell Stock II
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 点击率模型AUC
一 背景 首先举个例子: 正样本(90) 负样本(10) 模型1预测 ...
- 探索Windows命令行系列(7):通过命令编译C#类和Java类
1.编译 C# 类 1.1.C# 编译工具 1.2.编译一个 C# 类 1.3.编译多个 C# 类 2.编译 Java 类 2.1.Java 编译工具 2.2.编译 Java 类 3.组合命令符 4. ...
- Java Web - HTML 常用标签和符号
1.Html 注释,pre,<,>, ,超级链接,marquee,img标签 <html> <head> <title>常用 ...
- spring 框架的xml文件如何读取properties文件数据
spring 框架的xml文件如何读取properties文件数据 第一步:在spring配置文件中 注意:value可以多配置几个properties文件 <bean id="pro ...
- django Modelform
前言: 为什么要用form去验证呢? 我们提交的是form表单,在看前端源码时如果检查到POST URL及我们提交的字段,如果没有验证我们是否可以直接POST数据到URL,后台并没有进行校验,直接处理 ...
- java模拟一个抽奖程序
今天用一个程序模拟一个从1-32之间,随机抽取7组号码的抽奖程序 * 需要使用Java的图形界面知识 * 窗口 JFrame * 面板 JPanel * 显示文本信息的标签 JLabel * 文 ...
- java中常用的包、类、以及包中常用的类、方法、属性----sql和text\swing
java中常用的包.类.以及包中常用的类.方法.属性 常用的包 java.io.*; java.util.*; java.lang.*; java.sql.*; java.text.*; java.a ...