如何使用C#确定虚拟目录或网站的ASP.NET版本?
如何查找IIS虚拟目录的.NET framework在C#中的使用情况。我需要向用户显示它如何使用C#确定虚拟目录或网站的ASP.NET版本?,c#,asp.net,iis,directoryservices,C#,Asp.net,Iis,Directoryservices,如何查找IIS虚拟目录的.NET framework在C#中的使用情况。我需要向用户显示它 using System.DirectoryServices; using System.IO; private enum IISVersion { IIS5, IIS6 } private void ReadVirtualDirectory(string server, string directory, IISVersion version) { string siteID
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;
}
它可能不是最漂亮的方法,但如果您可以从aspnet\u 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
在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版本号。Nice代码。什么是正确的?它做错了什么?这不是回答了你的问题吗?如果没有,为什么不呢?您想知道什么.NET framework或什么版本的IIS?我想知道.NET framework。@Kev-无需回滚。这是我问题的一个更好的标题。谢谢你的解答。我将不得不修改它,以获得网站中每个虚拟目录的框架版本。