如果在win8,win7情况下报错:未知错误(0x80005000)

见http://blog.csdn.net/ts1030746080/article/details/8741399

using System;
using System.Collections;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;namespace IISControlHelper
{/// <summary>/// IIS 操作方法集合/// http://blog.csdn.net/ts1030746080/article/details/8741399 错误/// </summary>public class IISWorker{private static string HostName = "localhost";/// <summary>/// 获取本地IIS版本/// </summary>/// <returns></returns>public static string GetIIsVersion(){try{DirectoryEntry entry = new DirectoryEntry("IIS://" + HostName + "/W3SVC/INFO");string version = entry.Properties["MajorIISVersionNumber"].Value.ToString();return version;}catch (Exception se){//说明一点:IIS5.0中没有(int)entry.Properties["MajorIISVersionNumber"].Value;属性,将抛出异常 证明版本为 5.0return string.Empty;}}/// <summary>/// 创建虚拟目录网站/// </summary>/// <param name="webSiteName">网站名称</param>/// <param name="physicalPath">物理路径</param>/// <param name="domainPort">站点+端口,如192.168.1.23:90</param>/// <param name="isCreateAppPool">是否创建新的应用程序池</param>/// <returns></returns>public static int CreateWebSite(string webSiteName, string physicalPath, string domainPort,bool isCreateAppPool){DirectoryEntry root = new DirectoryEntry("IIS://" + HostName + "/W3SVC");// 为新WEB站点查找一个未使用的IDint siteID = 1;foreach (DirectoryEntry e in root.Children){if (e.SchemaClassName == "IIsWebServer"){int ID = Convert.ToInt32(e.Name);if (ID >= siteID) { siteID = ID + 1; }}}// 创建WEB站点DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", siteID);site.Invoke("Put", "ServerComment", webSiteName);site.Invoke("Put", "KeyType", "IIsWebServer");site.Invoke("Put", "ServerBindings", domainPort + ":");site.Invoke("Put", "ServerState", 2);site.Invoke("Put", "FrontPageWeb", 1);site.Invoke("Put", "DefaultDoc", "Default.html");// site.Invoke("Put", "SecureBindings", ":443:");site.Invoke("Put", "ServerAutoStart", 1);site.Invoke("Put", "ServerSize", 1);site.Invoke("SetInfo");// 创建应用程序虚拟目录DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir");siteVDir.Properties["AppIsolated"][0] = 2;siteVDir.Properties["Path"][0] = physicalPath;siteVDir.Properties["AccessFlags"][0] = 513;siteVDir.Properties["FrontPageWeb"][0] = 1;siteVDir.Properties["AppRoot"][0] = "LM/W3SVC/" + siteID + "/Root";siteVDir.Properties["AppFriendlyName"][0] = "Root";if (isCreateAppPool){DirectoryEntry apppools = new DirectoryEntry("IIS://" + HostName + "/W3SVC/AppPools");DirectoryEntry newpool = apppools.Children.Add(webSiteName, "IIsApplicationPool");newpool.Properties["AppPoolIdentityType"][0] = "4"; //4newpool.Properties["ManagedPipelineMode"][0] = "0"; //0:集成模式 1:经典模式newpool.CommitChanges();siteVDir.Properties["AppPoolId"][0] = webSiteName;}siteVDir.CommitChanges();site.CommitChanges();return siteID;}/// <summary>/// 得到网站的物理路径/// </summary>/// <param name="rootEntry">网站节点</param>/// <returns></returns>public static string GetWebsitePhysicalPath(DirectoryEntry rootEntry){string physicalPath = "";foreach (DirectoryEntry childEntry in rootEntry.Children){if ((childEntry.SchemaClassName == "IIsWebVirtualDir") && (childEntry.Name.ToLower() == "root")){if (childEntry.Properties["Path"].Value != null){physicalPath = childEntry.Properties["Path"].Value.ToString();}else{physicalPath = "";}}}return physicalPath;}/// <summary>/// 获取站点名/// </summary>public static List<IISInfo> GetServerBindings(){List<IISInfo> iisList = new List<IISInfo>();string entPath = String.Format("IIS://{0}/w3svc", HostName);DirectoryEntry ent = new DirectoryEntry(entPath);foreach (DirectoryEntry child in ent.Children){if (child.SchemaClassName.Equals("IIsWebServer", StringComparison.OrdinalIgnoreCase)){if (child.Properties["ServerBindings"].Value != null){object objectArr = child.Properties["ServerBindings"].Value;string serverBindingStr = string.Empty;if (IsArray(objectArr))//如果有多个绑定站点时{object[] objectToArr = (object[])objectArr;serverBindingStr = objectToArr[0].ToString();}else//只有一个绑定站点{serverBindingStr = child.Properties["ServerBindings"].Value.ToString();}IISInfo iisInfo = new IISInfo();iisInfo.DomainPort = serverBindingStr;iisInfo.AppPool = child.Properties["AppPoolId"].Value.ToString();//应用程序池iisList.Add(iisInfo);}}}return iisList;}public static bool CreateAppPool(string appPoolName, string Username, string Password){bool issucess = false;try{//创建一个新程序池DirectoryEntry newpool;DirectoryEntry apppools = new DirectoryEntry("IIS://" + HostName + "/W3SVC/AppPools");newpool = apppools.Children.Add(appPoolName, "IIsApplicationPool");//设置属性 访问用户名和密码 一般采取默认方式newpool.Properties["WAMUserName"][0] = Username;newpool.Properties["WAMUserPass"][0] = Password;newpool.Properties["AppPoolIdentityType"][0] = "3";newpool.CommitChanges();issucess = true;return issucess;}catch // (Exception ex) {return false;}}/// <summary>/// 建立程序池后关联相应应用程序及虚拟目录/// </summary>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();}}}}}}}/// <summary>/// 判断object对象是否为数组/// </summary>public static bool IsArray(object o){return o is Array;}}
}

  

转载于:https://www.cnblogs.com/webenh/p/8037607.html

C# 操作IIS方法集合相关推荐

  1. C#操作XML方法集合

    原文地址:http://www.cnblogs.com/zery/p/3362480.html 一 前言 先来了解下操作XML所涉及到的几个类及之间的关系  如果大家发现少写了一些常用的方法,麻烦在评 ...

  2. jQuery: 操作select option方法集合

    每一次操作select的时候,总是要谷歌一下资料,真是太不爽了, 在这里记录一下. 公共select代码 <select id="sel"><option val ...

  3. ASP.NET中文件上传下载方法集合

    asp.net 2008-08-23 21:10:35 阅读0 评论0   字号:大中小 订阅 ASP.NET中文件上传下载方法集合 文件的上传下载是我们在实际项目开发过程中经常需要用到的技术,这里给 ...

  4. Net中如何操作IIS

    Net中实际上已经为我们在这方面做得很好了.FCL中提供了不少的类来帮助我们完成这项工作,让我们的开发工作变非常简单和快乐.编程控制IIS实际上很简单,和ASP一样,.Net中需要使用ADSI来操作I ...

  5. Java入力项目无法设定到form_html中关于form与表单提交操作的资料集合

    原标题:html中关于form与表单提交操作的资料集合 这里我们介绍一下form元素与表单提交方面的知识. form元素 form元素的DOM接口是HTMLFormElement,继承自HTMLEle ...

  6. asp.net操作IIS主机头的问题总结

    今天发现一个怪怪的问题,在程序自动操作IIS主机头的问题上,在本地测试时没有任何问题,新增,删除主机头都正常.但传到服务器上时就有问题了,在新增主机头时没问题,删除主机头时就出现了这样的提示:拒绝访问 ...

  7. c++怎么将两个类的方法集合成一个类的方法_一文帮你梳理 Java 集合

    集合在我们日常开发使用的次数数不胜数,ArrayList/LinkedList/HashMap/HashSet······信手拈来,抬手就拿来用,在 IDE 上龙飞凤舞,但是作为一名合格的优雅的程序猿 ...

  8. day2.数据类型的操作和方法

    day2数据类型的操作和方法 1.字符串(str)的操作(不可变类型) 1.什么是字符 电子计算机或无线电通信中字母.数字和各种符号的统称. 2.什么是字符串 由数字.字母.下划线组成的一串字符. 3 ...

  9. 如何用光盘映像文件重装服务器系统,使用ISO系统镜像文件重装系统的方法集合...

    使用ISO系统镜像文件重装系统的方法集合 ISO系统镜像文件安装的方式一样可以分为很多种,和GHO文件的安装方法也有一些区别.GHO文件的安装方法相信不用多说,大家都很清楚.最简单的莫过于一键重装系统 ...

  10. C#中操作IIS 7.0

    C#中操作IIS 7.0 作者:Jaxu 出处:博客园  2009-3-26 10:17:52 阅读 53  次 <script type="text/javascript" ...

最新文章

  1. 莫烦老师的tensorflow降级方法
  2. JS的Document属性和方法
  3. 二维burgers方程_二维Burgers方程的RKDG有限元解法
  4. python字典副本_如何复制字典并仅在Python中编辑副本?
  5. ubuntu下nagios监控远程主机
  6. 在Ubuntu 18.04上安装PostgreSQL 11和PgAdmin4
  7. phpcms v9框架的目录结构分析
  8. 某度文库付费文档下载,实测可用~
  9. pandas读取数据库,将结果使用matplotlib绘制成二维表格图片
  10. python宿舍管理系统_python实现宿舍管理系统
  11. 全自动降噪插件-Acon Digital Extract:Dialogue 1.1.2 WiN-MAC
  12. tinder和bumble_发布课程:Tinder,Reddit,Airbnb,Etsy和Uber如何吸引了第一批用户
  13. html的lang属性
  14. excel 通过身份证 计算 年龄、性别
  15. 汽车ISP的“去留”之谜
  16. zzuli生化危机(dfs)
  17. Win10下安装Maven的解决方案
  18. 学习三部曲之(一):学生为什么学习不好?
  19. 海尔集团CEO张瑞敏演讲语录
  20. 主题挖掘和情感分析图书馆话题知乎用户问答行为数据

热门文章

  1. 10 signs that you’re not cut out to be an IT manager
  2. Python爬取豆瓣电影
  3. Spyder 常用操作
  4. matlab利用geotiffread读取tif文件报错:‘错误使用 tifflib, 无法打开 TIFF 文件’
  5. matlab学习笔记1
  6. mysql -f --force_MySQL force Index 强制索引概述
  7. 实习成长之路:DelayQueue多线程下的延迟队列的使用
  8. Python基础语法-03-私有化
  9. Android ContentProvider简单总结
  10. 个人成长过程中最重要的技能是什么?