[英]How to determine the clients .NET framework version in a web application?
I need to determine the clients .NET framework version in my web application. 我需要在我的Web应用程序中确定客户端.NET框架版本。 I'm running in partial trust so I can not read the filesystem or registry ( Is there an easy way to check .net framework verison using C#? ). 我正在部分信任,所以我无法读取文件系统或注册表( 有没有一种简单的方法来检查.net框架verison使用C#? )。
System.Environment.Version returns the runtime version, so I can not use that. System.Environment.Version返回运行时版本,因此我无法使用它。
I cannot use javascript 我不能使用javascript
The only way I can think of at the moment is to try to load a .NET 3.5 dll and catch an exception, but this does not sound very nice. 我现在能想到的唯一方法是尝试加载.NET 3.5 dll并捕获异常,但这听起来不太好。
Any suggestions? 有什么建议?
Update: 更新:
Request.Browser.ClrVersion
and Request.Browser.GetClrVersions()
will return the .NET framework version(s) installed on the client. Request.Browser.ClrVersion
和Request.Browser.GetClrVersions()
将返回客户端上安装的.NET框架版本。
You can use the Request.Browser.ClrVersion
property to get the client's highest .NET version and Request.Browser.GetClrVersions()
method to get all the installed .NET versions. 您可以使用Request.Browser.ClrVersion
属性获取客户端的最高.NET版本和Request.Browser.GetClrVersions()
方法以获取所有已安装的.NET版本。
These methods simply parse the Request.ServerVariables("HTTP_USER_AGENT")
server variable. 这些方法只是解析Request.ServerVariables("HTTP_USER_AGENT")
服务器变量。
But please note that a browser (or user or hacker) may put anything he wishes in the string, so you won't have 100% accuracy. 但请注意,浏览器(或用户或黑客)可能会将他想要的任何内容放入字符串中,因此您不会有100%的准确性。
I think you should do something like the following msdn article suggests . 我认为你应该做类似以下msdn文章建议的事情。 It uses java script to do the detection of .NET Framework. 它使用java脚本来检测.NET Framework。
One way could be to get the referenced assemblies list from current assembly. 一种方法是从当前程序集中获取引用的程序集列表。 And then look for mscorlib.dll (or any other .net assembly that you are sure is loaded) and get the version of that assembly. 然后查找mscorlib.dll(或您确定已加载的任何其他.net程序集)并获取该程序集的版本。 This way you would know the version of framework installed. 这样您就会知道安装的框架版本。
try this code: 试试这段代码:
Version version = null;
AssemblyName[] names = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
foreach (AssemblyName name in names)
{
if (name.Name == "mscorlib")
{
version = name.Version;
}
}
This all depends on the availability of the assembly that you choose to get the version from. 这一切都取决于您选择从中获取版本的程序集的可用性。
Or have a look at this CodeProject article. 或者看看这篇CodeProject文章。 In this article he/she gives reference to another article by Junfeng Zhang which uses unmanaged code to determine CLR version. 在本文中,他/她提供了张俊峰的另一篇文章,该文章使用非托管代码来确定CLR版本。
I use the following class, called directly from the Main() method entry point, and then have the choice to inform user through MessageBox or Console.WriteLine. 我使用以下类,直接从Main()方法入口点调用,然后可以选择通过MessageBox或Console.WriteLine通知用户。 This code is based on the fact that: 此代码基于以下事实:
static class DotNetFx35Checker {
[MethodImpl(MethodImplOptions.NoInlining)]
internal static bool IsDotNetFx35Available(out string failureReason, out string productName) {
productName = "MyProductName";
try {
TestLinqAvailable();
failureReason = null;
return true;
} catch (System.IO.FileNotFoundException) {
var productVersion = Assembly.GetExecutingAssembly().GetName().Version;
var productNameAndVersion = productName + " v" + productVersion.Major + "." + productVersion.Minor;
failureReason = "To run " + productNameAndVersion + ", please download and install .NET Fx v3.5 or upper version.";
return false;
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void TestLinqAvailable() {
var i = System.Linq.Enumerable.Count(new int[] { 1 });
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.