如何确定使用C#的虚拟目录或网站的ASP.NET版本? [英] How do I determine the ASP.NET version of a virtual directory or website using C#?

查看:90
本文介绍了如何确定使用C#的虚拟目录或网站的ASP.NET版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何找出一个IIS虚拟目录的.NET架构在C#中使用。我需要把它显示给用户。

 使用的System.DirectoryServices;
使用System.IO;私人枚举IISVersion
{
    IIS5,
    IIS6
}私人无效ReadVirtualDirectory(字符串服务器,字符串目录,IISVersion版)
{
    字符串的siteID =的String.Empty;
    路径字符串=的String.Empty;    开关号(Version)
    {
        案例IISVersion.IIS5:
            PATH =的String.Format(IIS:// {0} / SVC / 1 /根服务器);            如果(!string.IsNullOrEmpty(目录))
            {
                PATH = string.Concat(路径,/,目录);
            }            打破;        案例IISVersion.IIS6:
            PATH =的String.Format(IIS:// {0} / W3SVC,服务器);            如果(!string.IsNullOrEmpty(目录))
            {
                德的DirectoryEntry =新的DirectoryEntry(路径);                的foreach(的DirectoryEntry孩子de.Children)
                {
                    如果(child.Properties [ServerComment]。Value.ToString()。ToLower将()==目录)
                    {
                        的siteID = child.Name;
                        打破;
                    }
                }                PATH = string.Concat(路径,/的siteID);                de.Close()
                德= NULL;
            }            打破;
    }    iis的的DirectoryEntry =新的DirectoryEntry(路径);    //这里展出的iis性能...
    //需要显示如果虚拟目录下.NET 1.1或2.0上运行    iis.Close();
    非法入境= NULL;
}


解决方案

在IIS有找出哪些ASP.NET版本所使用的网站或虚拟目录(网站应用程序文件夹没有一成不变的方式 - 一个有齿轮作为一个图标)。

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

 使用系统;
使用的System.DirectoryServices;
命名空间的ConsoleApplication1
{
    类节目
    {
        静态无效的主要(字串[] args)
        {
            字符串路径= @IIS://本地主机/ W3SVC / 1 /根/ MyApp的;
            Console.WriteLine(GetAspNetVersion(路径));
        }        私人静态字符串GetAspNetVersion(字符串路径)
        {
            使用(的DirectoryEntry应用=新的DirectoryEntry(路径))
            {
                PropertyValueCollection PVC = app.Properties [脚本映射];                的foreach(聚氯乙烯串脚本映射)
                {
                    如果(scriptMap.StartsWith(ASPX))
                    {
                        串[]映射= scriptMap.Split(,);
                        串scriptProcessor ​​=映射[1];                        //版本号来自路径
                        // C:\\ WINDOWS \\ Microsoft.NET \\框架\\
                        //这将是在脚本处理器present
                        // DLL的路径
                        如果(scriptProcessor.Contains(V1.1.4322))
                        {
                            回到1.1;
                        }                        如果(scriptProcessor.Contains(V2.0.50727))
                        {
                            回到2.0;
                        }
                    }
                }
                抛出新的异常(未知版本的ASP.NET版本。);
            }
        }
    }
}

这是不理想,但并没有失败,我们需要为我们提供应用程序/服务的主机托管服务提供商。我怀疑IIS MMC插件执行幕后类似的检查。我自己的元数据库的详细检查(Windows 2000和Windows 2003文件基于XML)显示,没有一个明显的特性/属性存储特定ASP.NET版本号。

How do I find out the .NET framework of an IIS virtual directory is using in C#. I need to display it to the user.

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;       
}

解决方案

In IIS there's no hard and fast way to find out which ASP.NET version is used by a website or 'virtual' directory (website application folder - the one that has cogs as an icon).

The reason for this is that IIS and the metabase data only know about scriptmaps and not ASP.NET versions (after all ASP.NET is just another ISAPI application). One way to determine the version of ASP.NET is to use something like the following:

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.");
            }
        }
    }
}

It's not ideal but hasn't failed our needs as a hoster for our provisioning applications/services. I suspect the IIS MMC plug-in performs a similar check behind the scenes. My own detailed examination of the metabase (Windows 2000 and Windows 2003 file based XML) reveals that there isn't a conspicuous property/attribute that stores a specific ASP.NET version number.

这篇关于如何确定使用C#的虚拟目录或网站的ASP.NET版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆