473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Programmatically determine if .NET 3.0 is installed

I am writing a web app to be widely distributed where I do not know the
installed .NET Framework version. I want to take advantage of some .NET 3.0
classes if they are installed, but gracefully degrade if not available.
(Specifically, I want to use System.Windows.Media.Imaging.BitmapMetadata to
extract metadata from JPEG files when 3.0 is available, but will resort to
System.Drawing.Image if .NET 3.0 is not installed.)

What is the recommended best practice for determining whether .NET 3.0 is
installed? The solution must work in a Medium Trust environment. I am not
sure if there is a framework class for querying this info or if I should just
try accessing the class and let the exception tell me it is not available.

I have found that System.Environment.Version.ToString() gives 2.0.50727.1378
on my PC, so that doesn't help. (Unless the "1378" indicates that 3.0 is
installed, but I doubt it.)

Thanks!
Roger Martin
Aug 4 '07 #1
12 6588
I am sorry, but I need to determine the version running on the web server,
not on the client browser. The normal way would be to have code-behind refer
to System.Environment.Version.ToString(), but as I mentioned this returns
2.0.50727.1378, even when I have 3.0 installed.
Aug 4 '07 #2
Did you see this page on MSDN?

http://msdn2.microsoft.com/en-us/library/ms749404.aspx

Peter
--
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
bogMetaFinder: http://www.blogmetafinder.com

"Roger Martin" wrote:
I am writing a web app to be widely distributed where I do not know the
installed .NET Framework version. I want to take advantage of some .NET 3.0
classes if they are installed, but gracefully degrade if not available.
(Specifically, I want to use System.Windows.Media.Imaging.BitmapMetadata to
extract metadata from JPEG files when 3.0 is available, but will resort to
System.Drawing.Image if .NET 3.0 is not installed.)

What is the recommended best practice for determining whether .NET 3.0 is
installed? The solution must work in a Medium Trust environment. I am not
sure if there is a framework class for querying this info or if I should just
try accessing the class and let the exception tell me it is not available.

I have found that System.Environment.Version.ToString() gives 2.0.50727.1378
on my PC, so that doesn't help. (Unless the "1378" indicates that 3.0 is
installed, but I doubt it.)

Thanks!
Roger Martin
Aug 4 '07 #3
That's the code I quoted to him, Peter, but he wants server-side detection.

Here's how to detect the net framework version in a Visual Studio Setup and Deployment package:

http://support.microsoft.com/kb/315291

Aaron Stebner has useful C++ code to do that here :
http://astebner.sts.winisp.net/Tools/detectFX.cpp.txt

Migrating that to C# or VB.NET doesn't look too difficult.

Also, there's good C# code examples at :
http://www.codeproject.com/useritems...ndetection.asp

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"Peter Bromberg [C# MVP]" <pb*******@yahoo.yohohhoandabottleofrum.comwrote in message
news:20**********************************@microsof t.com...
Did you see this page on MSDN?

http://msdn2.microsoft.com/en-us/library/ms749404.aspx

Peter
--
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
bogMetaFinder: http://www.blogmetafinder.com

"Roger Martin" wrote:
>I am writing a web app to be widely distributed where I do not know the
installed .NET Framework version. I want to take advantage of some .NET 3.0
classes if they are installed, but gracefully degrade if not available.
(Specifically, I want to use System.Windows.Media.Imaging.BitmapMetadata to
extract metadata from JPEG files when 3.0 is available, but will resort to
System.Drawing.Image if .NET 3.0 is not installed.)

What is the recommended best practice for determining whether .NET 3.0 is
installed? The solution must work in a Medium Trust environment. I am not
sure if there is a framework class for querying this info or if I should just
try accessing the class and let the exception tell me it is not available.

I have found that System.Environment.Version.ToString() gives 2.0.50727.1378
on my PC, so that doesn't help. (Unless the "1378" indicates that 3.0 is
installed, but I doubt it.)

Thanks!
Roger Martin

Aug 4 '07 #4
Hi Roger,

Do you mean that you want to build your program against .NET Framework 2.0
but want to dynamically detect and load an assembly from .NET Framework
3.0? How will you use the System.Drawing.Image? Using reflection?

You can use registry to determine if .NET Framework 3.0 is installed or not:

http://msdn2.microsoft.com/en-US/lib...ywinfx_topic11

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


Aug 6 '07 #5
I am under the impression one cannot access the registry under Medium Trust,
but correct me if I am wrong.

I am building my web app and supporting class libraries against .NET 2.0,
but one of my class libraries has a reference to WindowsBase.dll and
PresentationCore.dll from .NET 3.0. When running this on a PC with .NET 2.0
*only*, the app runs fine and throws an exception only when directly invoking
the .NET 3.0 classes (i.e. System.Windows.Media.Imaging.BitmapMetadata).

That is why I would like to add some conditional logic to execute the 3.0
code only when .NET 3.0 is present, and avoid it when it is not. As I said
before, the solution must work under Medium Trust.

I think I can implement a solution by running the .NET 3.0 code in a try{}
block, but I started this thread to find out if there is a better, officially
recommended route. The solutions offered thus far do not work in Medium
Trust, and catching an exception is not very efficient, so I am hoping there
is a better way.

To answer your question about System.Drawing.Image, I access it directly and
not through reflection.

Roger Martin
Aug 9 '07 #6
On Aug 9, 8:20 pm, Roger Martin <Roger Mar...@community.nospamwrote:
but one of my class libraries has a reference to WindowsBase.dll and
PresentationCore.dll from .NET 3.0.
Hi Roger

the .NET 3 adds into context at least two new assemblies for WCF, and
this can be checked using AppDomain.GetAssemblies() method. If you
found one of these assemblies - .NET 3 is installed

Example:

System.Reflection.Assembly[] assemblies =
AppDomain.CurrentDomain.GetAssemblies();
foreach (System.Reflection.Assembly assembly in assemblies)
{
if (assembly.ManifestModule.Name.ToLower() ==
"system.runtime.serialization.dll")
..... // .net 3 is installed
}

In principle, in the same way you can test if PresentationCore or
other assembly has been loaded

System.Reflection.Assembly[] assemblies =
AppDomain.CurrentDomain.GetAssemblies();
foreach (System.Reflection.Assembly assembly in assemblies)
{
if (assembly.ManifestModule.Name.ToLower() == "presentationcore.dll")
.... // loaded
}

Hope it helps

Aug 9 '07 #7
Based on my understanding, what Roger is trying to do here is to have a
separate assembly that has references to .NET 3.0 framework, and he need to
detect if .NET 3.0 is installed or not before using/loading this assembly.
Therefore I don't think your method will work since the main assembly isn't
referencing .NET 3.0 framework and current AppDomain certainly has not
loaded any 3.0 assemblies.

Roger, you're right registry access is not allowed in medium trust. I
somehow overlooked this in your first message, sorry.

So far I'm not aware of any official way to achieve this, I'll need some
time to do some researching and consulting for you. I'll keep you posted.
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 13 '07 #8
Thanks for looking into this, Walter. I look forward to your response.

Interestingly, I thought Alexey may have given me the solution I needed by
using AppDomain.CurrentDomain.GetAssemblies() to discover if
presentationcore.dll was loaded. This actually works when the app is running
under Full Trust. My project has a static reference to presentationcore.dll,
and .NET seems to load it when it is present and not load it when not
present. If I place Alexey's code just before I reference .NET 3.0 specific
classes, I can avoid calling the classes when .NET 3.0 is not there. Works
great!

Alas, the technique fails under Medium Trust. During app startup I get the
following exception:

System.Security.Policy.PolicyException: Required permissions cannot be
acquired.

[HttpException (0x80004005): Could not load file or assembly
'PresentationCore, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35' or one of its dependencies. Failed to grant
minimum permission requests. (Exception from HRESULT: 0x80131417)]

So it seems my web app should not have a reference to presentationcore.dll
and instead I must dynamically load the dll only when .NET 3.0 is installed.
This is what Walter was suggesting in his last post, and it makes sense. I
hope we can figure out how to do this.

Cheers,
Roger Martin
Aug 14 '07 #9
Hi Roger,

Thanks for the update.

I've been consulting and researching on this issue recently. Here's the
summary so far:

Since ASP.NET in Medium trust by default doesn't have access to registry,
file system outside the virtual directory, and reflection: we will no
longer be able to detect .NET 3.0's presence using the normal way (that is
checking registry). Therefore I'm afraid the only option left is to use
try-catch to determine if your assembly that's using .NET 3.0 is ok to use
or not. I understand that you have some concerns on the performance issue
of this approach, my suggestion is that you can use a static class and
variable to do this once and shared by your entire web application:

public static class Class1
{

public static readonly bool IsMyAssemblyUsingNet3Avaiable;

static Class1()
{
try
{
MyClassUseNet3 c3 = new MyClassUseNet3();
IsMyAssemblyUsingNet3Avaiable = true;
}
catch (Exception)
{
IsMyAssemblyUsingNet3Avaiable = false;
}
}
}

A static variable in ASP.NET web application will live with the ASP.NET
worker process.

You need to remove those 3.0 assemblies references in web.config if they're
added by IDE when you reference the assembly that is referencing 3.0
framework.

I've tested this on my side with two systems (one with 3.0, one without)
and it seems it's working fine. The checking is only made once, later you
can directly use the Class1.IsMyAssemblyUsingNet3Available variable to
determine which class to use.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Aug 15 '07 #10
Hi Roger,

Previously my assumption is to move the code that're using 3.0 to a
separate assembly and the main web application doesn't need to reference
3.0 but it references the assembly directly. (Though when you add the
reference or compile the web application, it seems Visual Studio will try
to add those 3.0 assemblies' references into web.config, but removing them
are fine for the compiling). Sorry I didn't describe this more clearly.

I think your approach will work fine, as long as the CAS policy for the
"Medium" level grants the code to use reflection. Remember this policy can
be customized by administrator to include or remove certain permission set;
though the policy may still named as "Medium", it might not have the exact
required permissions by your application.

Based on my test, the default "Medium" policy will allow my code to use
Assembly.Load, but not assembly.GetType and other reflection operations.
Therefore, you may need to check with your web hosting environment to see
if they're using the required permissions in the CAS policy.

For more information, please refer to following MSDN library:

#How To: Use Medium Trust in ASP.NET 2.0
http://msdn2.microsoft.com/en-us/library/ms998341.aspx
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 17 '07 #11
I tried your suggestion to add a reference to my new assembly instead of
using reflection. It works, but there is a big annoyance in VS 2008 beta 2.
Every time I compile I get the following popup:

"You are attempting to add a reference to a project which is targeting .NET
Framework 3.0. The current target framework for your project is .NET
Framework 2.0. For this reference to work correctly, you may need to update
the target framework version to .NET Framework 3.0 using the property page
for your project. Do you want to add this project reference?"

Of course I answer No. I can't stand to deal with this every time I build,
so I'd like to revert to my reflection approach, but your statement that
GetType() is not available in medium trust alarmed me.

However, in my case I am successfully using assembly.GetType where
web.config has:

<trust level="Medium"/>

Based on my research it seems that GetType() will fail if accessing a
non-public type, but that it should work for public types. Since that is what
I am doing, I think I am safe.

If you have information to the contrary, please let me know.

Cheers,
Roger
Aug 17 '07 #12
Hi Roger,

Based on my test, if you call GetType on an assembly that is strong named,
it will succeed even in Medium trust leve. This is what you've observed
that "work for public types". Previously my test code hasn't signed that
assembly (which is using .NET 3.0).

For the VS2008 Beta 2 issue, I've also performed a test locally: based on
my findings, VS will only issue the warning when you add the reference, not
at every time when you build the project. In my test, I have a class
library that targets 3.0, a console application targets 2.0; then the
console app references the class library.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 20 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David Sworder | last post by:
Hi there, I'm designing an application in C#. One of the files in my application is "server.exe". This assembly listens for inbound connections and handles various incoming requests. I want to...
2
by: Jak Sparrow | last post by:
What are some good ways to programmatically check for software already installed on a computer? For example, to check a version of Internet Explorer, a program can check the registry for the...
2
by: NewsAccount | last post by:
Hi I'm trying to find a way to programmatically measure the size of the page sent down to the browser, can't seem to measure the length of what gets written to the HTML TextWriter in protected...
1
by: PetersonMike | last post by:
One gets countless hits when searching for discussions on the need to install IIS before .NET so that ISAPI references are setup properly. It's also documented that one can run apsnet_regiis.exe...
2
by: Mike C# | last post by:
Hi all, What's the "best" way to determine an EXE file's DLL dependencies from within a program? I basically need to figure out which DLL's are required by an EXE like Dependency Walker, from...
1
by: crjunk | last post by:
I'm trying to programmatically determine the maximum field length for an Access table. I've had problems using the examples that I have come across on the internet. Can anyone provide me with a...
14
by: mistral | last post by:
Need php script to create mySQL database programmatically; since hosting configuration may not allow create database from script, script also need eliminate/rewrite all restrictions in appropriate...
1
by: Superfreak3 | last post by:
We have a web application that will be installed as a virtual directory. It will either default to the Default Web Site or allow the choice of the site to which the vir. dir. will be installed, if...
1
by: Leonid Lastovkin | last post by:
I would like to be able to find out which version of Microsoft Office is installed on the host computer. How can I implement this query in C#? I do not need the whole program, but a relevant code...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.