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.

2
  • 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? Commented Mar 18, 2010 at 8:52
  • The .NET framework version of the client computer connecting to my web application. Commented Mar 18, 2010 at 9:02

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Nice, I also found this: Request.Browser.ClrVersion and Request.Browser.GetClrVersions() which will return the .NET framework version(s) installed on the client.
IE 11 on Win8.1 does not return any Clr information in the UA.
@Dan-o: Which is good, since IMO this leaks sensitive information.
@Steven: absolutely. No doubt about that. Just pointing out that your solution appears to only work with older systems. There's no criticism here.
@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.
|
3

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

4 Comments

useragent. ding ding ding. we have a winner.
As I said in the question I can not use javascript.
Can you explain why can't you use JavaScript?
Technically I can, but it's a customer requirement (don't ask why hehe). I'm looking for alternatives, and if there are no good ones, I'll have to discuss that with the customer.
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.

4 Comments

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.
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.
OK, looks like this the only solution under the given circumstances. Thank you.
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'.
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 });
   }
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.