欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#操作IIS方法集合

程序员文章站 2023-11-09 19:30:28
c# 操作iis方法集合 如果在win8,win7情况下报错:未知错误(0x80005000) ----见 using system; using syste...

c# 操作iis方法集合
如果在win8,win7情况下报错:未知错误(0x80005000) ----见

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 操作方法集合
  /// //www.jb51.net/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>
    public static bool isarray(object o)
    {
      return o is array;
    }
  }
}

希望本文所述对大家的c#程序设计有所帮助。