简体   繁体   English

如何在Web应用程序中确定客户端.NET框架版本?

[英]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.ClrVersionRequest.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: 此代码基于以下事实:

  • The class System.Linq.Enumerable appeared with .NET v3.5 System.Linq.Enumerable类出现在.NET v3.5中
  • This class is declared in the assembly System.Core.dll that also appeared with .NET v3.5 此类在程序集System.Core.dll中声明,该程序集也出现在.NET v3.5中
  • The exception FileNotFoundException is thrown when an assembly is no found. 找不到程序集时抛出异常FileNotFoundException

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.

相关问题 如何确定运行我的应用程序所需的最小.NET框架版本 - How to determine the minimal required .NET framework version to run my app 确定库 C# 中的应用程序框架版本 - Determine application framework version in library C# 如何在4.5 .NET Framework版本中构建Web API? - How to build web api in 4.5 .net framework version? 如何创建和执行.net Web服务客户端? - How to create and execute .net web service clients? 运行时使用的应用程序框架版本目标和Net Framework版本 - Application framework version target and Net Framework version used in runtime .NET技术中如何制作Win和Web应用程序的试用版 - how to make trial version of win and web application in .Net technology AntiXSS in .NET Framework 4.7 web 应用-如何应用 - AntiXSS in .NET Framework 4.7 web application - how to apply it 如何更新ASP.NET样板Web应用程序框架 - How to update ASP.NET boilerplate web application framework 在Windows Xp客户端上运行使用.NET 4.5开发的Web应用程序 - Running web application developed using .NET 4.5 on Windows Xp clients 确定对不正确 .NET Framework 版本的间接依赖的来源 - Determine the source of an indirect dependency on incorrect .NET Framework version
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM