• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#操作IIS方法集合

C#操作IIS方法集合

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了c站,c语言,欲情 c max,奔驰c200,85度c等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

C# 操作IIS方法集合
如果在win8,win7情况下报错:未知错误(0x80005000) ----见http://www.weikejianghu.com/article/72881.htm

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://www.weikejianghu.com/article/72881.htm 错误
  /// </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 = 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"; //4
        newpool.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>
   



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C# 检索不区分大小写并高亮显示实例详解
  • C#实现Base64处理的加密解密,编码解码示例
  • C# SqlHelper应用开发学习
  • C#多线程经典示例(吃苹果)
  • C#使用Windows Service的简单教程(创建、安装、卸载、调试)
  • C# 6.0 的知识梳理
  • C#向PPT文档插入图片以及导出图片的实例
  • C#使用Jquery zTree实现树状结构显示 异步数据加载
  • C#清理非托管对象实例分析
  • C#双缓冲技术实例详解

相关文章

  • 2017-05-28深入c# Func委托的详解
  • 2017-05-28C#用ComboBox控件实现省与市的联动效果的方法
  • 2017-05-28C#使用Protocol Buffer(ProtoBuf)进行Unity中的Socket通信
  • 2017-05-28C#数据结构与算法揭秘三 链表
  • 2017-05-28C# WinForm打开PDF文件并在窗体中显示
  • 2017-05-28基于C#实现一个最简单的HTTP服务器实例
  • 2017-05-28C#实现左截取和右截取字符串实例
  • 2017-05-28c#:CTS类型系统
  • 2017-05-28C#实现属于自己的QQ截图工具
  • 2017-05-28C#窗体间通讯处理的几种方法总结

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C# 全角和半角转换以及判断的简单代码
    • C#实现将类的内容写成JSON格式字符串的方法
    • 浅析C#中文件路径的操作
    • C#模板方法模式(Template Method Pattern)实例教程
    • 深入C#中get与set的详解
    • C#不登录电脑启动程序
    • C# 设计模式系列教程-工厂方法模式
    • C#中DataGridView动态添加行及添加列的方法
    • C#泛型实例详解
    • C#程序中使用LINQ to XML来查询XML格式数据的实例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有