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的更多相关文章

  1. C#操作IIS程序池及站点的创建配置

    最近在做一个WEB程序的安装包:对一些操作IIS进行一个简单的总结:主要包括对IIS进行站点的新建以及新建站点的NET版本的选择,还有针对IIS7程序池的托管模式以及版本的操作:首先要对Microso ...

  2. C#操作IIS完整解析

    原文:C#操作IIS完整解析 最近在为公司实施做了一个工具,Silverlight部署早已是轻车熟路, 但对于非技术人员来说却很是头疼的一件事,当到现场实施碰到客户情况也各不相同, 急需一个类似系统备 ...

  3. C#操作IIS程序池及站点的创建配置(转)

      原文:http://www.cnblogs.com/wujy/archive/2013/02/28/2937667.html 最近在做一个WEB程序的安装包:对一些操作IIS进行一个简单的总结:主 ...

  4. C#操作IIS服务

    进入正题:先从使用角度来讲解IIS操作,然后再深入到具体的IIS服务底层原理. [1]前提掌握要点: (1).IIS到目前经历了四个版本分别为 IIS4.0 IIS5.0 IIS6.0 IIS7.0, ...

  5. C# 使用代码来操作 IIS

    由于需要维护网站的时候,可以自动将所有的站点HTTP重定向到指定的静态页面上. 要操作 IIS 主要使用到的是“Microsoft.Web.Administration.dll”. 该类库不可以在引用 ...

  6. .net操作IIS,新建网站,新建应用程序池,设置应用程序池版本,设置网站和应用程序池的关联

    ServerManager类用来操作IIS,提供了很多操作IIS的API.使用ServerManager必须引用Microsoft.Web.Administration.dll,具体路径为:%wind ...

  7. 利用ASP.NET操作IIS (可以制作安装程序)

    很多web安装程序都会在IIS里添加应用程序或者应用程序池,早期用ASP.NET操作IIS非常困难,不过,从7.0开始,微软提供了 Microsoft.Web.Administration 类,可以很 ...

  8. c#操作IIS站点

    /// <summary> /// 获取本地IIS版本 /// </summary> /// <returns></returns> public st ...

  9. C#操作IIS程序池及站点的创建配置实现代码

    首先要对Microsoft.Web.Administration进行引用,它主要是用来操作IIS7: using System.DirectoryServices;using Microsoft.We ...

随机推荐

  1. c#通用多线程基类,以队列形式

    c#通用多线程基类,以队列形式 个人原创.欢迎转载.转载请注明出处.http://www.cnblogs.com/zetee/p/3487084.html 多线程这个概念大家都很熟悉,对于winfor ...

  2. golang 之 bson 与 struct 转换

    bson的介绍不说了golang下的解析包找到2个 一个是mongo的http://labix.org/gobson,另外一个比较小众https://github.com/sbunce/bson这里用 ...

  3. SolrCloud攻略

    SolrCloud攻略 近期一直在使用SolrCloud,乘着酒醉大概总结一下. 1.安装 原来一直有个误区,认为SolrCloud启动时,必须至少有个core才可以,其实不然. 首先按照Solr官方 ...

  4. 使用 ASP.NET MVC 4, EF, Knockoutjs and Bootstrap 设计和开发站点 - 6 - 业务逻辑

    翻译:使用 ASP.NET MVC 4, EF, Knockoutjs and Bootstrap 设计和开发站点 - 6 - 业务逻辑 Part 3: 设计逻辑层:核心开发 如前所述,我们的解决方案 ...

  5. linux前四天学习笔记

    以下是在linux培训机构所学的内容,感觉比较乱 MySQL学习笔记MySQL的安装 linux中的超级管理员rootaixocm vnc的退出: F8 MySQL的特点.优点:关系型开源.免费c++ ...

  6. continue与break

    1.continue语句,1至20内奇数累加和 #include<iostream> using namespace std; void main(){ int i=0; int sum= ...

  7. 2440开发板linux系统移植3G拨号上网收发短信(三)

    一.用text查看模式 下面的“发”是指我敲的命令,“收”是指回车后显示的信息包括其他接收的信息. ~ >: microcom -s 115200 /dev/ttyUSB1 发:at 收:OK ...

  8. Android 获得各处图片的方法

    <pre name="code" class="java">//1,已将图片保存到drawable目录下 //通过图片id获得Drawable Re ...

  9. crudandroidandroid——CRUD(在上一篇博客的基础上)

    废话就不多说了,开始... 1.Person package com.njupt.sqlite; public class Person { private Integer id; private S ...

  10. 开发一个微信小程序项目教程

    一.注册小程序账号 1.进入微信公众平台(https://mp.weixin.qq.com/),注册小程序账号,根据提示填写对应的信息即可.2.注册成功后进入首页,在 小程序发布流程->小程序开 ...