如果在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.0
return 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站点查找一个未使用的ID
int siteID = ;
foreach (DirectoryEntry e in root.Children)
{
if (e.SchemaClassName == "IIsWebServer")
{
int ID = Convert.ToInt32(e.Name);
if (ID >= siteID) { siteID = ID + ; }
}
}
// 创建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", );
site.Invoke("Put", "FrontPageWeb", );
site.Invoke("Put", "DefaultDoc", "Default.html");
// site.Invoke("Put", "SecureBindings", ":443:");
site.Invoke("Put", "ServerAutoStart", );
site.Invoke("Put", "ServerSize", );
site.Invoke("SetInfo");
// 创建应用程序虚拟目录 DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir");
siteVDir.Properties["AppIsolated"][] = ;
siteVDir.Properties["Path"][] = physicalPath;
siteVDir.Properties["AccessFlags"][] = ;
siteVDir.Properties["FrontPageWeb"][] = ;
siteVDir.Properties["AppRoot"][] = "LM/W3SVC/" + siteID + "/Root";
siteVDir.Properties["AppFriendlyName"][] = "Root"; if (isCreateAppPool)
{
DirectoryEntry apppools = new DirectoryEntry("IIS://" + HostName + "/W3SVC/AppPools"); DirectoryEntry newpool = apppools.Children.Add(webSiteName, "IIsApplicationPool");
newpool.Properties["AppPoolIdentityType"][] = ""; //
newpool.Properties["ManagedPipelineMode"][] = ""; //0:集成模式 1:经典模式
newpool.CommitChanges();
siteVDir.Properties["AppPoolId"][] = 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[].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"][] = Username;
newpool.Properties["WAMUserPass"][] = Password;
newpool.Properties["AppPoolIdentityType"][] = "";
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;
}
}
}

C# 操作IIS方法集合的更多相关文章

  1. js操作textarea方法集合

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  2. C#操作XML方法集合

    一 前言 先来了解下操作XML所涉及到的几个类及之间的关系  如果大家发现少写了一些常用的方法,麻烦在评论中指出,我一定会补上的!谢谢大家 * 1 XMLElement 主要是针对节点的一些属性进行操 ...

  3. 【分享】 封装js操作textarea 方法集合(兼容很好)。

    请使用下面的btn操作. 虽然你现在看来没什么用,当要用的时候又到处找资料,还不如现在收集一下.         在DOM里面操作textarea里面的字符,是比较麻烦的. 于是我有这个封装分享给大家 ...

  4. C#操作IIS完整解析

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

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

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

  6. JavaScript数组的一些方法集合

    数组方法集合 push()添加到数组末尾,并返回修改后数组的长度 var a=array.push('a','b'); alert(a);//2 pop() 移除数组最后一项,返回移除的项. shif ...

  7. 【转载】Windows Server 2012服务器删除IIS方法

    在Windows Server2012版本的服务器系统中,我们可以通过服务器管理器中的"添加角色和功能"来添加IIS的Web服务器,当我们不再使用IIS功能时候,我们也可以通过删除 ...

  8. 第52节:String,权限修饰符,方法,集合

    String String str1 = "dashu"; String str2 = "dashu"; String string = new String( ...

  9. python对redis的常用操作 下 (无序集合,有序集合)

    无序集合: 首先介绍增加,删除和获得所有元素的方法.我将会用第二部分来讨论集合的特殊操作: In [136]: x.sadd("challenge", 1,2,3,4,5,6,7, ...

随机推荐

  1. Jquery Md5加密解密

    首先需要调用md5解析的js文件.(右击-目标另存为方式下载) http://files.cnblogs.com/files/colinliu/md5.js 加密方法参考: <script ty ...

  2. 面试集锦-常量,const, const 对指针的影响

    在C语言中不可改变的数据(量)就是常量    在C语言中有三种常量        字面量(直接量),就是直接写出来的,从写法上就可以看出值与类型等,例如:19,123.456等        名字常量 ...

  3. POJ 3071 Football

    很久以前就见过的...最基本的概率DP...除法配合位运算可以很容易的判断下一场要和谁比.    from——Dinic算法                         Football Time ...

  4. 资源URL地址记录

    1. 如何搭建VPN服务器 http://www.360doc.com/content/11/0217/11/3084932_93749130.shtml http://www.softxp.net/ ...

  5. ubuntu下修改apache2.4的rewrite

    sudo a2enmod rewrite 修改/etc/apache2/apache2.conf中 AllowOverride None 为 AllowOverride ALL 重启 service ...

  6. CSS Image Sprite--网页图片应用处理方式

    CSS Sprites简介 CSSSprites在国内很多人叫css精 灵,是一种网页图片应用处理方式.它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片 ...

  7. ExtJs xtype类型介绍

    自定义组件在定义的时候可以通过xtype配置为组件指定xtype短名称,此后创建对象可以通过xtype来创建自定义对象了,示例代码如下: Ext.define('MyApp.PressMeButton ...

  8. bug-android之INSTALL_FAILED_NO_MATCHING_ABIS无法安装在虚拟机

    bug描述: 经常在网络上下载一些实例,自己研究 ,运行时不时会出现这个bug: Installation error: INSTALL_FAILED_NO_MATCHING_ABIS bug解决方案 ...

  9. BZOJ 2574: [Poi1999]Store-Keeper

    Description 推箱子. \(n,m\leqslant 100\) Sol Tarjan+边双连通分量+BFS. 直接搜索的复杂度是 \(n^6\) 记录人的位置,箱子的位置和转移. 箱子的位 ...

  10. 6 HandlerDescriptor 处理程序描述类——Live555源码阅读(一)基本组件类

    这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...