tags:

views:

83

answers:

3

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.

+3  A: 

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

Ikaso
useragent. ding ding ding. we have a winner.
Sky Sanders
As I said in the question I can not use javascript.
Ezombort
Can you explain why can't you use JavaScript?
Ikaso
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.
Ezombort
+2  A: 

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.

cornerback84
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
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.
cornerback84
OK, looks like this the only solution under the given circumstances. Thank you.
Ezombort
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
+2  A: 

You can read the user agent string from the HTTP variables on the server:

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.

Steven
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
Haha... that's a good one. I didn't even know it existed. Nice.
Steven