2

如何找出在 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;       
}
4

2 回答 2

5

它可能不是最漂亮的方法,但如果您可以获取并解析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
于 2009-03-11T22:56:41.087 回答
5

在 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 版本号的显着属性/属性。

于 2009-03-11T23:29:45.360 回答