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. CODEFORCES#274 DIV2

    A[傻逼题] 大意:给你a,b,c三个数,你可以在其中加上括号,加号,乘号,使得到的值最大 就是问你 a+b+c,a*(b+c),(a+b)*c,a*b*c,(a+c)*b 哪个最大! 我去...这不 ...

  2. Day3:T1数论+高精 T2搜索

    T1:数论+高精(水~) 根据题意可知,从除的数越大越好(在0~9中) 所以我们只要用到高精除然后再模拟一下就可以了 //MARK:但是要注意0-9这个特殊值需要特判,因为题目要求输出的数至少是两位数 ...

  3. CMStepCounter Class Refernce

    CMStepCounter Class Refernce https://developer.apple.com/library/ios/documentation/CoreMotion/Refere ...

  4. ie7,8下__flash__addCallback报错原因及解决方案

    首先附张图,以直观说明错误的现象:(ie7.8)

  5. 译文:User-agent的历史

      这是一篇译文,译文出处在文章底部贴出.由于技术水平,英语水平,翻译水平有限,请各路大侠多多指正,谢谢,提高你也提高我:)   几个礼拜之前,我谈论了特征检测和浏览器检测.这篇帖子提到了一点点嗅探U ...

  6. ibatis实战之基础环境搭建

    关于ibatis的介绍.优缺点,以及ibatis和hibernate的比较再此不在赘述,可参阅其他资料. 一.准备工作 1.下载ibatis软件包http://download.csdn.net/de ...

  7. CentOS_6.5_x64:VNC安装配置

    1.安装软件前首先检查下系统是否已经安装了这个软件:rpm -qa tigervnc-server 2.根据前面命令的查询,显示系统还是没有安装VNC服务器端软件,那么我们就使用命令进行安装一下:yu ...

  8. nginx错误:unknown directive "锘? in F:\nginx/conf/nginx.conf:3

    C:\Users\Administrator>d: D:\>cd D:\nginx-1.4.7 D:\nginx-1.4.7>start nginx.exe D:\nginx-1.4 ...

  9. Python基础1-变量、运算符、表达式

    一.Python的安装 1.下载python安装包https://www.python.org/ 2.选择对应的Python版本(Windows下) 3.装完之后打开电脑的cmd,验证一下安装是否成功 ...

  10. C语言之形参和实参

    一 形参与实参 1).定义 形参:形式参数. 就是定义函数的时候,写在函数名小括号后面的参数叫形参 实参:实际参数. 就是调用函数的时候,调用函数时写在函数名小括号后面的参数就叫实参 2).注意项 a ...