【问题标题】:How do I determine the ASP.NET version of a virtual directory or website using C#?如何使用 C# 确定虚拟目录或网站的 ASP.NET 版本?
【发布时间】:2009-03-11 22:04:50
【问题描述】:

如何找出在 C# 中使用的 IIS 虚拟目录的 .NET 框架。我需要向用户展示它。

using System.DirectoryServices;
using System.IO;

private enum IISVersion
{
    IIS5,
    IIS6
}

private void ReadVirtualDirectory(string server, string directory, IISVersion version)
{
    string siteID = string.Empty;
    string path = string.Empty;

    switch(verison)
    {
        case IISVersion.IIS5:
            path = string.Format("IIS://{0}/W3SVC/1/Root", server);

            if(!string.IsNullOrEmpty(directory))
            {
                path = string.Concat(path, "/", directory);
            }

            break;

        case IISVersion.IIS6:
            path = string.Format("IIS://{0}/W3SVC", server);

            if(!string.IsNullOrEmpty(directory))
            {
                DirectoryEntry de = new DirectoryEntry(path);

                foreach(DirectoryEntry child in de.Children)
                {
                    if(child.Properties["ServerComment"].Value.ToString().ToLower() == directory)
                    {
                        siteID = child.Name;
                        break;
                    }
                }

                path = string.Concat(path, "/", siteID);

                de.Close()
                de = null;
            }

            break;
    }

    DirectoryEntry iis = new DirectoryEntry(path);

    //display iis properties here...
    //need to display if the virtual directory is running under .NET 1.1 or 2.0

    iis.Close();
    iis = null;       
}

【问题讨论】:

  • 不错的代码。它做对了什么?它做错了什么?它没有回答你问的问题吗?如果没有,为什么不呢?
  • 你想知道什么.NET框架或什么版本的IIS?
  • 我想了解.NET框架。
  • @Kev - 无需回滚。这对我的问题来说是一个更好的标题。

标签: c# asp.net iis directoryservices


【解决方案1】:

在 IIS 中,没有硬性和快速的方法来找出网站或“虚拟”目录(网站应用程序文件夹 - 以 cogs 作为图标的文件夹)使用哪个 ASP.NET 版本。

原因是 IIS 和元数据库只知道脚本映射而不知道 ASP.NET 版本(毕竟 ASP.NET 只是另一个 ISAPI 应用程序)。确定 ASP.NET 版本的一种方法是使用如下内容:

using System;
using System.DirectoryServices;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"IIS://Localhost/W3SVC/1/root/MyApp";
            Console.WriteLine(GetAspNetVersion(path));
        }

        private static string GetAspNetVersion(string path)
        {
            using (DirectoryEntry app = new DirectoryEntry(path))
            {
                PropertyValueCollection pvc = app.Properties["ScriptMaps"];

                foreach (string scriptMap in pvc)
                {
                    if (scriptMap.StartsWith(".aspx"))
                    {
                        string[] mapping = scriptMap.Split(',');
                        string scriptProcessor = mapping[1];

                        // The version numbers come from the path
                        // C:\Windows\Microsoft.NET\Framework\
                        // which will be present in the script processor 
                        // DLL path
                        if (scriptProcessor.Contains("v1.1.4322"))
                        {
                            return "1.1";
                        }

                        if (scriptProcessor.Contains("v2.0.50727"))
                        {
                            return "2.0";
                        }
                    }
                }
                throw new Exception("Unknown version ASP.NET version.");
            }
        }
    }
}

这并不理想,但并没有满足我们作为供应应用程序/服务的托管商的需求。我怀疑 IIS MMC 插件在幕后执行了类似的检查。我自己对元数据库(基于 Windows 2000 和 Windows 2003 文件的 XML)的详细检查表明,没有存储特定 ASP.NET 版本号的显着属性/属性。

【讨论】:

  • 感谢您的解决方案。我将不得不对其进行修改以获取网站中每个虚拟目录的框架版本。
【解决方案2】:

这可能不是最漂亮的方法,但如果您可以获取并解析 aspnet_regiis.exe -lk 的输出,您将获得所需的所有信息。 SharePoint VHD 的输出示例:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>aspnet_Regiis -lk
W3SVC/  1.1.4322.2407
W3SVC/1/ROOT/Reports/   2.0.50727.0
W3SVC/1/ROOT/ReportServer/      2.0.50727.0
W3SVC/1619303638/Root/  2.0.50727.0
W3SVC/1619303638/Root/_layouts/images/  1.1.4322.2407
W3SVC/1619303638/Root/_layouts/inc/     1.1.4322.2407
W3SVC/1720207907/root/  2.0.50727.0
W3SVC/1720207907/root/SharedServices1/  2.0.50727.0
W3SVC/1848312571/Root/  2.0.50727.0
W3SVC/1848312571/Root/_layouts/images/  1.1.4322.2407
W3SVC/1848312571/Root/_layouts/inc/     1.1.4322.2407
W3SVC/784543535/Root/   2.0.50727.0
W3SVC/784543535/Root/_layouts/images/   1.1.4322.2407
W3SVC/784543535/Root/_layouts/inc/      1.1.4322.2407
W3SVC/98413328/Root/    2.0.50727.0
W3SVC/98413328/Root/_layouts/images/    1.1.4322.2407
W3SVC/98413328/Root/_layouts/inc/       1.1.4322.2407

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多