Ezombort
Ezombort

Reputation: 1912

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. 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#?).

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.

Upvotes: 9

Views: 2731

Answers (5)

Amit Sharma
Amit Sharma

Reputation: 163

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

Upvotes: 0

Steven
Steven

Reputation: 172606

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.

Upvotes: 5

Patrick from NDepend team
Patrick from NDepend team

Reputation: 13842

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

Upvotes: 1

ata
ata

Reputation: 9011

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.

Upvotes: 2

Ikaso
Ikaso

Reputation: 2268

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

Upvotes: 3

Related Questions