views:

496

answers:

5

Here's the thing. My .NET application crashes with a pretty ugly general exception fault when I try to run it in a machine that doesn't have .NET framework installed.

Is this normal? If it is... is there any way to check for .NET framework to be able to exit gracefully instead?

A: 

You have to do that check in the installer script, I suppose.

Bharani
+2  A: 

You could write a stub in non-managed code to do it, but prerequisites are normally why you create setup applications.

Steven Robbins
+6  A: 

You can't check the version number of the .NET framework with managed code as it can't execute before loading the .NET runtime. You can use the CLR Unmanaged API to do so, but the best way is to solve this problem is to provide an installation mechanism that checks, downloads and installs .NET Framework if it's not installed on the machine.

Mehrdad Afshari
+1  A: 

Take a look at the .NET Framework Client Profile (http://msdn.microsoft.com/en-us/library/cc656912.aspx):

The .NET Framework Client Profile provides a common bootstrapper setup that you can use for your client applications. This makes sure that all requirements for running your application are installed, regardless of which version of the .NET Framework, if any, is present. The setup experience provides a consistent user interface (UI) and seamless installation, whether the target operating system is Windows XP or Windows Vista.

Guido Domenici
A: 

If it's packaged in an installer you can set the install conditions to check for .NET and the framework version.

The other way to check the framework is:

System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory;
System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion;

From the O'Reilly Cookbook.

But both of those are defeated by the fact that .NET has to be installed before the code can even begin to check.

Joe Chin