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

C#实现获取IIS站点及虚拟目录信息的方法

程序员文章站 2023-10-31 14:16:04
本文实例讲述了c#实现获取iis站点及虚拟目录信息的方法。分享给大家供大家参考。具体如下: using system; using system.directo...

本文实例讲述了c#实现获取iis站点及虚拟目录信息的方法。分享给大家供大家参考。具体如下:

using system;
using system.directoryservices;
using system.collections.generic;
using system.text;
namespace consoleapplication1
{
  class program
  {
    static void main(string[] args)
    {
      directoryentry rootentry = new directoryentry("iis://localhost/w3svc");
      int siteid = 1;
      foreach (directoryentry entry in rootentry.children)
      {
        if (entry.schemaclassname.equals("iiswebserver", stringcomparison.ordinalignorecase))
        {
          console.writeline("name: {0}", entry.name);
          console.writeline("path: {0}", iisworker.getwebsitephysicalpath(entry));
          console.writeline("serverbindings: {0}", entry.properties["serverbindings"].value);
          console.writeline();
          directoryentry virentry = new directoryentry(entry.path + "/root");
          foreach (directoryentry entryvirtual in virentry.children)
          {
            if (entryvirtual.schemaclassname.equals("iiswebvirtualdir", stringcomparison.ordinalignorecase))
            {
              console.writeline("schemaclassname: {0}", entryvirtual.schemaclassname);
              console.writeline("name: {0}", entryvirtual.name);
              console.writeline("path: {0}", entryvirtual.properties["path"].value);
              console.writeline();
            }
          }
          int id = convert.toint32(entry.name);
          if (id >= siteid)
          {
            siteid = id + 1;
          }
        }
      }
    }
  }
  public class iisworker
  {
    /// <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;
    }
  }
}

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