开发者

How to determine the clients .NET framework version in a web application?

开发者 https://www.devze.com 2022-12-23 13:54 出处:网络
I need to determine the clients .NET framework version in my web application. I\'m running in partial trust so I can not read the filesystem or registry (Is there an easy way to check .net framework v

I need to determine the clients .NET framework version in my web application. 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#?).

  • System.Environment.Version returns the runtime version, so I can not use that.

  • I cannot use 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.

Any suggestions?

Update开发者_Go百科:

Request.Browser.ClrVersion and Request.Browser.GetClrVersions() will return the .NET framework version(s) installed on the client.


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.

These methods simply parse the Request.ServerVariables("HTTP_USER_AGENT") server variable.

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.


I think you should do something like the following msdn article suggests. It uses java script to do the detection of .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. 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. In this article he/she gives reference to another article by Junfeng Zhang which uses unmanaged code to determine CLR version.


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. This code is based on the fact that:

  • The class System.Linq.Enumerable appeared with .NET v3.5
  • This class is declared in the assembly System.Core.dll that also appeared with .NET v3.5
  • The exception FileNotFoundException is thrown when an assembly is no found.

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


You can use the browser's network tab. Load any page off the application and then in the network tab, choose any request made to server. Then in the Headers => Response Headers section of the request, you can see the X-AspNet-Version:

How to determine the clients .NET framework version in a web application?

0

精彩评论

暂无评论...
验证码 换一张
取 消