在 ASP.NET(2.0 及以上版本)中,我们可以使用 System.DirectoryServices 来管理 IIS。

System.DirectoryServices 可以获取和设置 IIS metabase 的 String and DWORD 属性,并且也能调用大多数方法。

前提是您的操作系统应该是 Windows XP Professional 带 SP2,或 Windows Server 2003 带 SP1 或更新版本的操作系统。否则您可能会得到类似“The directory cannot report the number of properties”的错误。

不过开始之前,您应该在网站解决方案的属性页上 References 中引入 System.DirectoryServices。添加后,web.config 中应该有类似如下的内容:

<configuration>
<system.web>
<compilation debug="false">
<assemblies>
<add assembly="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

另外还请您注意一下,您运行 ASP.NET 的帐户要有足够的权限来操作 IIS。

关于用 System.DirectoryServices 来管理 IIS 的所有内容请参见 MSDN 上的 http://msdn.microsoft.com/en-us/library/ms525791.aspx。

另外我摘录了一点示例,是关于如何创建站点、设置端口、创建虚拟目录,虽然是 Console 环境中的,但只需要作些很简单的修改,就可以应用到 ASP.NET 中。

using System;
using System.IO;
using System.DirectoryServices;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections;

namespace System_DirectoryServices_DirectoryEntry_ConfigIIS
{
class Program
{
static void Main(string[] args)
{

...

CreateSite("IIS://Localhost/W3SVC", "555", "MySite", "D:\\Inetpub\\Wwwroot");

...

SetSingleProperty("IIS://Localhost/W3SVC/555", "ServerBindings", ":8080:");

...

CreateVDir("IIS://Localhost/W3SVC/1/Root", "MyVDir", "D:\\Inetpub\\Wwwroot");

...

}

...

static void CreateSite(string metabasePath, string siteID, string siteName, string physicalPath)
{
//  metabasePath is of the form "IIS://<servername>/<service>"
//    for example "IIS://localhost/W3SVC"
//  siteID is of the form "<number>", for example "555"
//  siteName is of the form "<name>", for example, "My New Site"
//  physicalPath is of the form "<drive>:\<path>", for example, "C:\Inetpub\Wwwroot"
Console.WriteLine("\nCreating site {0}/{1}, mapping the Root application to {2}:",
metabasePath, siteID, physicalPath);

try
{
DirectoryEntry service = new DirectoryEntry(metabasePath);
string className = service.SchemaClassName.ToString();
if (className.EndsWith("Service"))
{
DirectoryEntries sites = service.Children;
DirectoryEntry newSite = sites.Add(siteID, (className.Replace("Service", "Server")));
newSite.Properties["ServerComment"][0] = siteName;
newSite.CommitChanges();

DirectoryEntry newRoot;
newRoot = newSite.Children.Add("Root", "IIsWebVirtualDir");
newRoot.Properties["Path"][0] = physicalPath;
newRoot.Properties["AccessScript"][0] = true;
newRoot.CommitChanges();

Console.WriteLine(" Done. Your site will not start until you set the ServerBindings or SecureBindings property.");
}
else
Console.WriteLine(" Failed. A site can only be created in a service node.");
}
catch (Exception ex)
{
Console.WriteLine("Failed in CreateSite with the following exception: \n{0}", ex.Message);
}
}

...

static void SetSingleProperty(string metabasePath, string propertyName, object newValue)
{
//  metabasePath is of the form "IIS://<servername>/<path>"
//    for example "IIS://localhost/W3SVC/1"
//  propertyName is of the form "<propertyName>", for example "ServerBindings"
//  value is of the form "<intStringOrBool>", for example, ":80:"
Console.WriteLine("\nSetting single property at {0}/{1} to {2} ({3}):",
metabasePath, propertyName, newValue, newValue.GetType().ToString());

try
{
DirectoryEntry path = new DirectoryEntry(metabasePath);
PropertyValueCollection propValues = path.Properties[propertyName];
string oldType = propValues.Value.GetType().ToString();
string newType = newValue.GetType().ToString();
Console.WriteLine(" Old value of {0} is {1} ({2})", propertyName, propValues.Value, oldType);
if (newType == oldType)
{
path.Properties[propertyName][0] = newValue;
path.CommitChanges();
Console.WriteLine("Done");
}
else
Console.WriteLine(" Failed in SetSingleProperty; type of new value does not match property");
}
catch (Exception ex)
{
if ("HRESULT 0x80005006" == ex.Message)
Console.WriteLine(" Property {0} does not exist at {1}", propertyName, metabasePath);
else
Console.WriteLine("Failed in SetSingleProperty with the following exception: \n{0}", ex.Message);
}
}

...

static void CreateVDir(string metabasePath, string vDirName, string physicalPath)
{
//  metabasePath is of the form "IIS://<servername>/<service>/<siteID>/Root[/<vdir>]"
//    for example "IIS://localhost/W3SVC/1/Root"
//  vDirName is of the form "<name>", for example, "MyNewVDir"
//  physicalPath is of the form "<drive>:\<path>", for example, "C:\Inetpub\Wwwroot"
Console.WriteLine("\nCreating virtual directory {0}/{1}, mapping the Root application to {2}:",
metabasePath, vDirName, physicalPath);

try
{
DirectoryEntry site = new DirectoryEntry(metabasePath);
string className = site.SchemaClassName.ToString();
if ((className.EndsWith("Server")) || (className.EndsWith("VirtualDir")))
{
DirectoryEntries vdirs = site.Children;
DirectoryEntry newVDir = vdirs.Add(vDirName, (className.Replace("Service", "VirtualDir")));
newVDir.Properties["Path"][0] = physicalPath;
newVDir.Properties["AccessScript"][0] = true;
// These properties are necessary for an application to be created.
newVDir.Properties["AppFriendlyName"][0] = vDirName;
newVDir.Properties["AppIsolated"][0] = "1";
newVDir.Properties["AppRoot"][0] = "/LM" + metabasePath.Substring(metabasePath.IndexOf("/", ("IIS://".Length)));

newVDir.CommitChanges();

Console.WriteLine(" Done.");
}
else
Console.WriteLine(" Failed. A virtual directory can only be created in a site or virtual directory node.");
}
catch (Exception ex)
{
Console.WriteLine("Failed in CreateVDir with the following exception: \n{0}", ex.Message);
}
}

...

}
}

转载于:https://www.cnblogs.com/n666/archive/2009/12/15/2190979.html

用 ASP.NET 管理 IIS(转)相关推荐

  1. [翻译] ASP.NET内幕 - IIS处理模型

    原文地址:ASP.NET Internals – IIS and the Process Model 2007.05.03 Simone Busoli ASP.NET是开发Web应用和组建的漂亮的框架 ...

  2. ASP.NET 获取IIS应用程序池的托管管道模式

    asp.net 中怎样较为简单的获取网站程序池的托管管道模式 目前已知的方式是根据这个帖子https://github.com/kakalotte/... ,利用DirectoryEntry,但是程序 ...

  3. 【转】ASP.NET内幕 - IIS处理模型

    介绍 微软的Active ServerPages,即ASP,自1996年首次发布以来,为Web开发者构建Web应用提供了一个丰富.复杂的框架.过去的几年它的基础架构发展的如此迅速,成为目前大家了解的A ...

  4. 服务器的管理IIS 6.0

    IIS 6.0 和 Windows Server 2003在网络应用服务器的管理.可用性.可靠性.安全性.性能与可扩展性方面提供了许多新的功能.IIS 6.0同样增强了网络应用的开发与国际性支持.II ...

  5. ASP.NET管理状态的十种途径

    HTTP协议是无状态的,ASP.NET提供了丰富的手段在页面之间管理状态.本文列举ASP.NET管理状态的十种途径. ASP.NET中,从System.Web.UI.Page继承的类里有以下十种管理页 ...

  6. ASP.NET在IIS上部署使用Oracle数据库无法连接数据库解决方法

    ASP.NET在IIS上部署使用Oracle数据库无法连接数据库解决方法(转载) 10小时前 ASP.NET在IIS上部署使用Oracle数据库无法连接数据库解决方法(转载) 分类: ASP.NET| ...

  7. 利用Advanced Installer将asp.netMVC连同IIS服务和mysql数据库一块打包成exe安装包

    原文:利用Advanced Installer将asp.netMVC连同IIS服务和mysql数据库一块打包成exe安装包 因为业务需要,项目中需要把asp.netmvc项目打包成exe安装程序给客户 ...

  8. iis与mysql关联_利用Advanced Installer将asp.netMVC连同IIS服务和mysql数据库一

    利用Advanced Installer将asp.netMVC连同IIS服务和mysql数据库一 利用Advanced Installer将asp.netMVC连同IIS服务和mysql数据库一块打包 ...

  9. 【Windows Server 2019】Web服务 IIS 配置与管理—— IIS 的安装与基本配置 Ⅲ

    目录 4. 安装 IIS 服务器 5. IIS 的基本配置 5.1 绑定 IP 参考资料 关联博文 4. 安装 IIS 服务器 准备工作:选择一台服务器作为WEB-IIS服务器,IP地址为192.16 ...

  10. asp.net配置IIS过程错误解决

    转载:http://zjcxyxy.blog.163.com/blog/static/9005992520131125105626409/ 1.安装IIS.在控制面版安装后,发现没有IIS管理功能,重 ...

最新文章

  1. 计算机视觉方向简介 | 阵列相机立体全景拼接
  2. Connectify错误“Internet Connection Sharing is currently unavailable.”解决方法不要有多重的网桥连接
  3. Python应用matplotlib绘图简介
  4. Estimathon
  5. 【Python基础】13个知识点,系统整理Python时间处理模块Datetime
  6. spring javafx_带有Spring的JavaFX 2
  7. Go语言学习Day05
  8. 研究大学生基础课程成绩和专业课程成绩的关系,证明两者之间是否有线性关系
  9. win10系统的快捷键
  10. nbu备份nas文件服务器,NBU网络备份大全之远程配置备份策略
  11. PS学习笔记--去掉图片上不想要的部分
  12. python 金融量化盘后分析系统V0.48
  13. VRRP配置与维护手册-1
  14. 隧道管廊UWB定位系统解决方案
  15. 微信公众平台开发(2)--微信认证流程图文详解
  16. Python中find_elements以及presence_of_element_located的用法
  17. 05流量管理原理-3金丝雀TCP流量整形比例分配
  18. Rotator和Vector之间的转换
  19. 易推影视推手系统,支持苹果v8 v10影视系统
  20. 大学生、办公人员电脑必备的10款实用软件 简直是太好用了

热门文章

  1. Asp.net 基础(二)
  2. 电信网通南北分治 学者呼吁应查处
  3. 两年以后重读了一篇文章,写了点东西。
  4. 如何理解Spring中的IOC和AOP
  5. Py之pandas:dataframe学习【转载】
  6. BUG报告:habahaba风格,图片显示有问题
  7. linux自建git仓库
  8. http各类攻击及tcpcopy工具
  9. Android之开发杂记(一)
  10. 话里话外:明白比智慧更重要