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;
        }
    }
}
时间: 2024-08-05 17:04:04

C# 获取IIS站点及虚拟目录信息的相关文章

Apache配置站点与虚拟目录

一.单站点配置: 在apache中,配置一个站点是最基础的配置,在这里我只是要配置一个最基础的WEB站点,因此配置方式也是 非常简单,具体配置方式如下: #########主站点配置########### #侦听端口 Listen 80 #站点域名 ServerName www.test.cn #站点文件主目录 DocumentRoot /data/www/test #该站点限制 <Directory /> AllowOverride None Order deny,allow Deny fr

C#获取IIS所有站点及虚拟目录和应用程序(包含名称及详细信息)

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.DirectoryServices; using System.Diagnostics; namespac

IIS下 多站点 利用虚拟目录 访问共用(图片)文件夹

预期目的:站点A 和 站点B 共用同一个图片文件夹C 实现方式是: 在站点下添加虚拟目录 使虚拟目录指向共用文件夹 站点下的页面 使用图片的访问方式示意:<image src="C/1.png" />其中 C为虚拟目录 IIS的站点布局示意: --网站----Default Web Site------应用程序A http://localhost/A/index.html--------虚拟目录C 指向D:\www\C------应用程序B http://localhost

导出IIS Log列表,导出站点下虚拟目录列表

Add-Type -AssemblyName System.Web import-module webadministration $ip = (gwmi Win32_NetworkAdapterConfiguration -Filter "IPEnabled='true'" | ? {$_.DefaultIPGateway -ne $null}).IPAddress[0] #导出所有站点的IISLog目录列表到IISLog.htm文件 $html = Get-Website|Sele

给 IIS Express 配置虚拟目录

使用 vs2015 打开旧项目,之前使用 iis 配置站点,然后在 vs 中附加 w3wp.exe 进行开发和调试的. 由于种种原因 iis 上配置站点各种失败. 之后发现,其实在 vs2015 中按 f5 会更方便,但是这个旧项目使用了虚拟目录.然后就一顿研究,先试了 System.Web.Routing 各种不行... 最后发现,原因可以直接配置... 右击右下角 IIS Express -> 显示所有应用 -> 选择站点 -> 配置 .vs\config\applicationho

IIS6中给Framework2,。0站点的虚拟目录(2.0版本)下发布Web API项目(4.0版本)问题处理

Web-API项目以虚拟目录形式部署到IIS6/IIS7 若原有站点为Framework2.0版本,在此站点(或虚拟目录站点)下,新增API虚拟目录,然后选择Framework4.0版本,IIS6和IIS7配置有所不同. IIS6配置: 1)        在主站A中的web.config中,添加<location>节点,要包住<system.web> 和<system.codedom>,节点内容如下: <location path="." a

iis express添加虚拟目录

在调试WEB时,还是使用IIS EXPRESS比较方便, 在IIS中,选择网站,右击,添加虚拟目录或者应用程序,就能添加虚拟目录了.. 在IIS EXPRESS中,添加虚拟目录如下 1.右击IIS E 的图标,点击显示所有应用程序 2.选择那个要加虚拟目录的测试网站,点击配置 3.会在VS中打开这个文件applicationhost.config 找到site节,这个节下有很多网站,根据名字找到要配置的那个,增加下图中红框所示的节,修改剪头所指的关键属性 即虚拟目录的名字,虚拟目录的物理地址 4

#Apache#多站点与虚拟目录配置

1.修改配置文件 Apache\conf\httpd.conf 找到 LoadModule vhost_alias_module modules/mod_vhost_alias.so     去掉前面的# 找到 Include conf/extra/httpd-vhosts.conf                                               去掉前面的# 2.多站点配置 Apache\conf\extra\httpd-vhosts.conf 增加(几个站点就增加

[php]修改站点的虚拟目录

wamp默认的站点的目录是www的目录,可以修改appache的httpd.conf文件来修改目录,修改方法如下: 1. <Directory "D:/SoftWare/wamp/www/"> 修改为:<Directory "D:/Develop/project/php"> 2.注销documentroot目录 #DocumentRoot "D:/SoftWare/wamp/www/" 注:加#即使注释