C# 操作IIS -App & AppPools
using System;
using System.DirectoryServices;
using System.Management;
using Microsoft.Web.Administration; namespace ConsoleApplication_IsNullTest
{
/// <summary>
/// IIS Helper.
/// </summary>
public class IISHelper
{ private static string siteName = "siteName";
private static string sitePath = "sitePath";
private static string serverId = "";
private static string runtimeVersion="v4.0"; public static string RuntimeVersion
{
get { return runtimeVersion; }
} public static DirectoryEntry iisSite = new DirectoryEntry("IIS://localhost/W3SVC", "UserName", "Userpwd"); private static ManagementScope scope = null; public IISHelper()
{
scope = new ManagementScope("ConfigManager.ManagementScopePath");
if (scope.IsConnected == false)
{
scope.Connect();
}
} /// <summary>
/// 创建应用程序池
/// </summary>
/// <param name="AppPoolName">程序池名</param>
/// <param name="PipelineModel">Managed pipeline model.</param>
public static void CreateAppPool(string AppPoolName, ManagedPipelineMode pipelineMode)
{
DirectoryEntry newpool;
DirectoryEntry apppools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
newpool = apppools.Children.Add(AppPoolName, "IIsApplicationPool");
newpool.Properties["ManagedRuntimeVersion"].Value = runtimeVersion;
newpool.Properties["ManagedPipelineMode"].Value = ManagedPipelineMode.Integrated;
newpool.CommitChanges();
} /// <summary>
/// 创建site
/// </summary>
public static void CreateWebSite(Object site)
{
ManagementObject manage = new ManagementObject(scope, new ManagementPath(@"IIsWebService='W3SVC'"), null);
ManagementBaseObject inputParameters = manage.GetMethodParameters("CreateNewSite");
ManagementBaseObject[] serverBinding = new ManagementBaseObject[];
serverBinding[] = CreateServerBinding("", "127.0.0.1", "");
inputParameters["ServerComment"] = siteName;
inputParameters["ServerBindings"] = serverBinding;
inputParameters["PathOfRootVirtualDir"] = sitePath;
inputParameters["ServerId"] = serverId;
try
{
manage.InvokeMethod("CreateNewSite", inputParameters, null);
}
catch
{
throw new Exception("createNewSite");
}
SetAppToPool(siteName, serverId); System.Threading.Thread.Sleep(); //启动web site
string serverName = "W3SVC/" + serverId;
ManagementObject manageSite = new ManagementObject(scope, new ManagementPath(@"IIsWebServer='" + serverName + "'"), null);
manageSite.InvokeMethod("Start", null);
} /// <summary>
/// 关联应用程序池
/// </summary>
/// <param name="appPoolName"></param>
/// <param name="serverId"></param>
private static void SetAppToPool(string appPoolName, string serverId)
{
string path = string.Format("IIS://localhost/W3SVC/{0}/Root", serverId);
DirectoryEntry entry = new DirectoryEntry(path);
entry.Properties["AppPoolId"].Value = appPoolName;
entry.CommitChanges();
entry.Close();
} /// <summary>
/// Get site url前缀
/// </summary>
/// <param name="HostName"></param>
/// <param name="IP"></param>
/// <param name="Port"></param>
/// <returns></returns>
private static ManagementObject CreateServerBinding(string HostName, string IP, string Port)
{
try
{
ManagementClass classBinding = new ManagementClass(scope, new ManagementPath("ServerBinding"), null);
ManagementObject serverBinding = classBinding.CreateInstance();
serverBinding.Properties["Hostname"].Value = HostName;
serverBinding.Properties["IP"].Value = IP;
serverBinding.Properties["Port"].Value = Port;
serverBinding.Put();
return serverBinding;
}
catch
{
return null;
}
} /// <summary>
/// 创建web site 子项的site $ 应用程序池
/// </summary>
/// <param name="site"></param>
/// <param name="serviceName"></param>
/// <param name="AppolName"></param>
public static void CreateApplication(Object site, string serviceName, string AppolName)
{
DirectoryEntry defaultWebSite = GetWebisteDirectory("FUSiteName");//父级site name
if (defaultWebSite != null)
{
DirectoryEntry defaultWebSiteRoot = new DirectoryEntry(defaultWebSite.Path + "/Root"); //Create and setup new virtual directory
DirectoryEntry virtualDirectory = defaultWebSiteRoot.Children.Add(siteName, "IIsWebVirtualDir"); virtualDirectory.Properties["Path"][] = sitePath;
virtualDirectory.Properties["AppFriendlyName"][] = siteName;
virtualDirectory.CommitChanges(); virtualDirectory.Invoke("AppCreate", );
object[] param = { , siteName, true };
virtualDirectory.Invoke("AppCreate3", param);
virtualDirectory.Properties["AppPoolId"].Value = AppolName; string appPoolPath = @"IIS://localhost/W3SVC/AppPools/" + AppolName;
try
{
var appPoolEntry = new DirectoryEntry(appPoolPath);
appPoolEntry.Properties["ManagedRuntimeVersion"].Value = runtimeVersion;
appPoolEntry.Properties["ManagedPipelineMode"].Value = ManagedPipelineMode.Integrated;
appPoolEntry.Invoke("SetInfo", null);
appPoolEntry.CommitChanges();
appPoolEntry.Close();
}
catch
{ }
virtualDirectory.CommitChanges();
virtualDirectory.Close();
}
} /// <summary>
/// Get web site directory.
/// </summary>
/// <param name="websiteName"></param>
/// <returns></returns>
private static DirectoryEntry GetWebisteDirectory(string websiteName)
{
DirectoryEntries des = iisSite.Children;
foreach (DirectoryEntry sub in des)
{
if (sub.SchemaClassName == "IIsWebServer" && sub.Properties["ServerComment"].Contains(websiteName.Trim()))
{ return sub;
}
}
return null;
}
}
}
C# 操作IIS -App & AppPools的更多相关文章
- C#操作IIS程序池及站点的创建配置
最近在做一个WEB程序的安装包:对一些操作IIS进行一个简单的总结:主要包括对IIS进行站点的新建以及新建站点的NET版本的选择,还有针对IIS7程序池的托管模式以及版本的操作:首先要对Microso ...
- C#操作IIS完整解析
原文:C#操作IIS完整解析 最近在为公司实施做了一个工具,Silverlight部署早已是轻车熟路, 但对于非技术人员来说却很是头疼的一件事,当到现场实施碰到客户情况也各不相同, 急需一个类似系统备 ...
- C#操作IIS程序池及站点的创建配置(转)
原文:http://www.cnblogs.com/wujy/archive/2013/02/28/2937667.html 最近在做一个WEB程序的安装包:对一些操作IIS进行一个简单的总结:主 ...
- C#操作IIS服务
进入正题:先从使用角度来讲解IIS操作,然后再深入到具体的IIS服务底层原理. [1]前提掌握要点: (1).IIS到目前经历了四个版本分别为 IIS4.0 IIS5.0 IIS6.0 IIS7.0, ...
- C# 使用代码来操作 IIS
由于需要维护网站的时候,可以自动将所有的站点HTTP重定向到指定的静态页面上. 要操作 IIS 主要使用到的是“Microsoft.Web.Administration.dll”. 该类库不可以在引用 ...
- .net操作IIS,新建网站,新建应用程序池,设置应用程序池版本,设置网站和应用程序池的关联
ServerManager类用来操作IIS,提供了很多操作IIS的API.使用ServerManager必须引用Microsoft.Web.Administration.dll,具体路径为:%wind ...
- 利用ASP.NET操作IIS (可以制作安装程序)
很多web安装程序都会在IIS里添加应用程序或者应用程序池,早期用ASP.NET操作IIS非常困难,不过,从7.0开始,微软提供了 Microsoft.Web.Administration 类,可以很 ...
- c#操作IIS站点
/// <summary> /// 获取本地IIS版本 /// </summary> /// <returns></returns> public st ...
- C#操作IIS程序池及站点的创建配置实现代码
首先要对Microsoft.Web.Administration进行引用,它主要是用来操作IIS7: using System.DirectoryServices;using Microsoft.We ...
随机推荐
- 【分享】LateX排版软件学习教程合集
来源于:http://www.hejizhan.com/html/xueke/416/x416_13.html LATEX2e科技排版指南.pdf 8.3 MB An Example LaTeX ...
- java class load
https://mp.weixin.qq.com/s?__biz=MjM5NzMyMjAwMA==&mid=403638649&idx=2&sn=4f17e8b58c64875 ...
- 附加被分离DB
如何附加被分离的质疑数据库? 简介 有些时间,由于日志损坏等原因,导致了数据库质疑.如果此时你分离了数据库,那你会发现你无法再附加上数据库,那后果还是很严重的.因此本文提供了一种方式,可以使得当数 ...
- probuf了解
人们一直在强调,同 XML 相比, Protobuf 的主要优点在于性能高.它以高效的二进制方式存储,比 XML 小 3 到 10 倍,快 20 到 100 倍. 对于这些 “小 3 到 10 倍”, ...
- Memcached缓存入门篇
Asp.Net中使用Couchbase——Memcached缓存入门篇 前言 本文的主要目的就是简单的进行使用Memcached.这是Memchahed的官网http://memcached.org/ ...
- 有关sort函数的用法
最近碰到这个sort函数,网上查了一些资料,感觉还是直接扔给我代码比较好理解些 要是像我一样的童鞋,建议看这里:http://www.cplusplus.me/265.html 个人认为很好理解..这 ...
- Spring in action (1)
spring中注入bean的方法: 1.通过xml文件来注入bean. 2.通过java注解来注入 默认的bean生命周期是单例的.每次都会返回相同的类的实例.
- ASP.NET网站单独
解决ASP.NET网站单独发布指定页面问题 目录 前提 开始 aspx.cs文件放到单独的类库项目 一个可选择勾选页面的发布工具:LimusicAddin 前提 Asp.net 发布分为:动态编译和预 ...
- 构建一个真实的应用电子商务SportsStore(十)
构建一个真实的应用电子商务SportsStore(十) 我们现在还需要为管理员提供一个途径,使他能方便的管理网站的商品目录,这也是所有网站都需要的功能,常用到了几乎所有开发人员都要开发这种功能的地步, ...
- Winform 单实例运行
Winform 单实例运行 前言 前两天在博客园看到<如何防止程序多次运行>,文章写的很好,最后还留下一个问题给我们思考.关于Winform的防止多次运行,曾经也想研究过,但是后来工作上没 ...