如何使用C#确定虚拟目录或网站的ASP.NET版本?

我如何找到IIS虚拟目录的.NET框架正在C#中使用。 我需要将它显示给用户。

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

在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版本号。

它可能不是最漂亮的方法,但如果你可以抓取并解析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