winrom 实现在动配置IIS站点及程序池
来源:互联网 发布:怎么给淘宝宝贝配图 编辑:程序博客网 时间:2024/05/16 04:36
参考文档:
http://www.cnblogs.com/cxd4321/p/4192407.html
http://blog.csdn.net/a497785609/article/details/27802685
需导入的命名空间:
using System.Security.AccessControl;
using Microsoft.Web.Administration;
using System.DirectoryServices;
using System.Diagnostics;
using System.Management;
http://www.cnblogs.com/cxd4321/p/4192407.html
http://blog.csdn.net/a497785609/article/details/27802685
需导入的命名空间:
using System.Security.AccessControl;
using Microsoft.Web.Administration;
using System.DirectoryServices;
using System.Diagnostics;
using System.Management;
#region 获取本地IIS版本
public static void GetIIsVersion()
{
try
{
DirectoryEntry entry = new DirectoryEntry("IIS://localhost/W3SVC/INFO");
string version = "本地IIS版本为:IIS" + entry.Properties["MajorIISVersionNumber"].Value.ToString();
Console.WriteLine(version);
}
catch (Exception se)
{
Console.WriteLine(se);
}
}
#endregion
#region 创建应用程序池
/// <summary>
/// 创建引用程序池
/// </summary>
public static void CreateNewWebSite()
{
DirectoryEntry getEntity = new DirectoryEntry("IIS://localhost/W3SVC/INFO");
int Version = int.Parse(getEntity.Properties["MajorIISVersionNumber"].Value.ToString());
if (Version > 6)
{
string AppPoolName = "LabManager";
//检测是否存在
if (!IsAppPoolName(AppPoolName))
{
DirectoryEntry newpool;
DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
newpool = appPools.Children.Add(AppPoolName, "IIsApplicationPool");
newpool.CommitChanges();
}
#region 修改应用程序的配置(包含托管模式及其NET运行版本)
ServerManager sm = new ServerManager();
sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion = "v4.0";
sm.ApplicationPools[AppPoolName].ManagedPipelineMode = ManagedPipelineMode.Classic; //托管模式Integrated为集成 Classic为经典
sm.CommitChanges();
#endregion
}
}
#region MyRegion 检测应用程序池是否存在
private static bool IsAppPoolName(string AppPoolName)
{
bool result = false;
DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
//for循环寻找应用程序池下的所有子节点
foreach (DirectoryEntry getdir in appPools.Children)
{
if (getdir.Name.Equals(AppPoolName))
{
result = true;
break;
}
}
return result;
}
#endregion
#endregion
#endregion
#region 创建父站点
public static void CreateFatherWeb()
{
DirectoryEntry getdir = new DirectoryEntry("IIS://localhost/W3SVC");
foreach (DirectoryEntry getentity in getdir.Children)
{
if (getentity.SchemaClassName.Equals("IIsWebServer"))
{
foreach (DirectoryEntry getchild in getentity.Children)
{
if (getchild.SchemaClassName.Equals("IIsWebVirtualDir"))
{
foreach (DirectoryEntry getsite in getchild.Children)
{
if (getsite.Name.Equals("bbbb"))
{
getsite.DeleteTree();
Console.WriteLine("应用程序已存在!");
Console.WriteLine("执行删除操作!");
Console.WriteLine("删除成功!");
break;
}
}
}
}
}
}
string constIISWebSiteRoot = "IIS://localhost/W3SVC/1/ROOT";
DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
DirectoryEntry tbEntry = root.Children.Add("bbbb", "IIsWebVirtualDir");
tbEntry.Properties["Path"][0] = @"C:\inetpub\wwwroot\bbbb"; //虚拟目录物理路径
tbEntry.Invoke("AppCreate", true);//删除tbEntry.Invoke("AppDelete",true); tbEntry.CommitChanges();
Console.WriteLine("创建成功!");
//给父站点指定应用程序池名
// SetAppToPool("父站点名","Classic .NET AppPool");
}
#endregion
#region 创建子站点
public static void CreatChildNewWeb()
{
Boolean IsExits = false;
DirectoryEntry getdir = new DirectoryEntry("IIS://localhost/W3SVC");
foreach (DirectoryEntry getentity in getdir.Children)
{
if (getentity.SchemaClassName.Equals("IIsWebServer"))
{
foreach (DirectoryEntry getchild in getentity.Children)
{
if (getchild.SchemaClassName.Equals("IIsWebVirtualDir"))
{
foreach (DirectoryEntry getsite in getchild.Children)
{
//判断是否存在父站点
if (getsite.Name.Equals("ajaxtest"))
{
IsExits = true;
//循环父站点
foreach (DirectoryEntry child in getsite.Children)
{
//判断父站点中是否存在子站点
if (child.Name.Equals("aaaaaaa"))
{
Console.WriteLine("存在该子站点!");
Console.WriteLine("执行删除操作!");
child.DeleteTree();
Console.WriteLine("删除成功!");
break;
}
}
//路径格式:C:\inetpub\wwwroot + 站点名 + WebFrame
var url = @"C:\inetpub\wwwroot\ajaxtest\aaaaaaa";
string constIISWebSiteRoot = "IIS://localhost/W3SVC/1/ROOT/ajaxtest";
DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
DirectoryEntry tbEntry = root.Children.Add("aaaaaaa", "IIsWebVirtualDir");
tbEntry.Properties["Path"][0] = url;
tbEntry.Invoke("AppCreate", true);
Console.WriteLine("子站点创建成功!");
//给子站点指定应用程序池名
// SetAppToPool("子站点名","ASP.NET v4.0 Classic");
}
}
}
}
}
}
if (IsExits == false)
{
Console.WriteLine("不存在父站点,请先建立父站点");
}
}
#endregion
#endregion
#region 给站点设置应用程序池
/// <summary>
/// 给父站点指定应用程序池
/// </summary>
/// <param name="appname">站点名</param>
/// <param name="poolName">应用程序池名</param>
public static void SetAppToPool(string appname, string poolName)
{
//获取目录
DirectoryEntry getdir = new DirectoryEntry("IIS://localhost/W3SVC");
foreach (DirectoryEntry getentity in getdir.Children)
{
if (getentity.SchemaClassName.Equals("IIsWebServer"))
{
//设置应用程序程序池 先获得应用程序 在设定应用程序程序池
//第一次测试根目录
foreach (DirectoryEntry getchild in getentity.Children)
{
if (getchild.SchemaClassName.Equals("IIsWebVirtualDir"))
{
//找到指定的虚拟目录.
foreach (DirectoryEntry getsite in getchild.Children)
{
if (getsite.Name.Equals(appname))
{
//【测试成功通过】
getsite.Properties["AppPoolId"].Value = poolName;
getsite.CommitChanges();
Console.WriteLine("父站点应用程序池设置成功!");
}
}
}
}
}
}
}
public static void SetChildAppToPool(string appname, string poolName)
{
//获取目录
DirectoryEntry getdir = new DirectoryEntry("IIS://localhost/W3SVC");
foreach (DirectoryEntry getentity in getdir.Children)
{
if (getentity.SchemaClassName.Equals("IIsWebServer"))
{
//设置应用程序程序池 先获得应用程序 在设定应用程序程序池
//第一次测试根目录
foreach (DirectoryEntry getchild in getentity.Children)
{
if (getchild.SchemaClassName.Equals("IIsWebVirtualDir"))
{
//找到指定的虚拟目录.
foreach (DirectoryEntry getsite in getchild.Children)
{
if (getsite.Name.Equals("ajaxtest"))
{
foreach (DirectoryEntry child in getsite.Children)
{
//判断父站点中是否存在子站点
if (child.Name.Equals("aaaaaaa"))
{
//【测试成功通过】
getsite.Properties["AppPoolId"].Value = poolName;
getsite.CommitChanges();
Console.WriteLine("子站点应用程序池设置成功!");
}
}
}
}
}
}
}
}
}
阅读全文
0 0
- winrom 实现在动配置IIS站点及程序池
- C#操作IIS程序池及站点的创建配置实现代码
- C#操作IIS程序池及站点的创建配置实现代码
- JSP环境配置:Resin+iis 整合及多站点配置
- IIS站点配置进阶
- window7下用IIS建站点及配置
- Windows8.1中IIS服务安装及站点配置
- 在iis中创建和配置ftp站点
- C#实现获取IIS站点及虚拟目录信息的方法
- C#实现获取IIS站点及虚拟目录信息的方法
- iis下手动配置php
- IIS配置站点兼容性指令
- Windows IIS 站点安全配置
- IIS中配置WCF站点
- 安装IIS和配置站点
- IIS站点配置问题汇总
- 【zabbix】Windows服务器获取IIS站点以及程序池状态
- 【转载】JSP环境配置:Resin+iis 整合及多站点配置
- DFS优化,数学题,时间复杂度计算题
- 2、树莓派3 Model B ——— 使用GIT工具安装wiringPi
- QT实现简单的抽奖界面
- 工作快俩年了
- 使用MATLAB的MCC命令生成C/C++程序
- winrom 实现在动配置IIS站点及程序池
- java-行为型模式-策略设计模式
- AngularX 路由最佳实践
- linux svn 基础使用命令
- Jquery基本操作
- CMake教程
- C#中子类与父类的相互转换
- Java程序设计环境概述
- 关于vue-router中的query动态传参问题的解决