9

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:

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

Community
  • 1
  • 1
Ezombort
  • 1,892
  • 3
  • 16
  • 20
  • You are talking about 'the clients .NET framework version'. Do you mean the version of the client computer connecting to your web application, or the .NET framework version your current process runs in? – Steven Mar 18 '10 at 08:52
  • The .NET framework version of the client computer connecting to my web application. – Ezombort Mar 18 '10 at 09:02

5 Answers5

5

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.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • Nice, I also found this: Request.Browser.ClrVersion and Request.Browser.GetClrVersions() which will return the .NET framework version(s) installed on the client. – Ezombort Mar 19 '10 at 09:17
  • 1
    IE 11 on Win8.1 does not return any Clr information in the UA. – Sam Axe Feb 20 '15 at 12:48
  • @Dan-o: Which is good, since IMO this leaks sensitive information. – Steven Feb 20 '15 at 12:55
  • 1
    @Steven: absolutely. No doubt about that. Just pointing out that your solution appears to only work with older systems. There's no criticism here. – Sam Axe Feb 20 '15 at 12:57
  • @Dan-o: Didn't take it as such. Thanks for pointing this out. This is very useful information for anyone that thinks about using this method. – Steven Feb 20 '15 at 13:03
  • This solution does not work anymore. – jstuardo Dec 07 '21 at 18:03
3

I think you should do something like the following msdn article suggests. It uses java script to do the detection of .NET Framework.

Ikaso
  • 2,268
  • 19
  • 26
2

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.

ata
  • 8,853
  • 8
  • 42
  • 68
  • It is a 2.0 app. So all assemblies return version 2.0. I have a component that requires 3.5 and I want to display a message if the requirement is not met. – Ezombort Mar 18 '10 at 10:31
  • That components must have reference of mscorlib version 3.5. If it cannot load it, it will throw an exception. This seems to be the only method if you cannot use registry, filesystem, or javascripts. – ata Mar 18 '10 at 10:52
  • OK, looks like this the only solution under the given circumstances. Thank you. – Ezombort Mar 18 '10 at 11:06
  • 1
    I'm very curious how this can be an answer to his question, because he needs the .NET framework version of 'the client computer connecting to my web application'. – Steven Mar 19 '10 at 07:09
1

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 });
   }
}
Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
0

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:

Sample Screehnshot

Amit Sharma
  • 133
  • 8