How to avoid FileNotFoundException if .NET 3.5 is not installed?

If you try to run the .NET 3.5 application on a Windows computer that does not have this version of the .NET Framework installed, you will get a FileNotFoundException for some system assemblies (for example, System.Core 3.5.0.0).

Is it possible to catch this exception and tell the user to upgrade their .NET platform or use it too soon?

+3
source share
3 answers

How are you deploying the application now? ClickOnce can perform build checks (GAC) before launch, and with msi you should have the full set of pre-validation options available ... although this is not always possible, can you consider one of these deployment options?

Repeating the exception - just be sure to split Main so that it does nothing except throw an exception - otherwise JIT can stop loading Main:

 // [STAThread] here if winform [MethodImpl(MethodImplOptions.NoInlining)] static void Main() { try { MainCore(); } catch (SomeException ex) { // TODO something simple but fun } } static void MainCore() { ... } // your app here... 

If you put too much on the external Main, it can charter before starting any of them, since JITs may need types.

+3
source share

The simplest thing is to try. (I do not have any non-standard testing machines, but I assume that you do.)

Make your entry point very simple, which simply tries to load System.Core.dll and handles the exception accordingly. If it passes, go to another class that can then use it. If this fails, enter the appropriate error message and exit.

I suspect that you do not need to have this level of isolation - if you do not have fields that are not available, I would not expect the assembly to be allowed until you first call the method that needs it. I need to consult the CLR through C # for verification. However, keeping it fairly isolated is likely to be safer - this will avoid accidentally introducing dependencies later. Hell, you can even have your type of โ€œdownload and checkโ€ in a separate assembly that did nothing but launch another, if everything is in order.

+3
source share

You can catch this exception, yes.

In your main routine, just use try..catch around your main message loop.

 try { Application.Run(new MainForm()); } catch (Exception ex) { if (ex.MessageContains("Could not load file or assembly 'System.Core, Version=3.5.0.0")) { MessageBox.Show("This product requires the Microsoft .NET Framework version 3.5, or greater, in order to run.\n\nPlease contact your System Administrator for more information."); } } 
+2
source share

All Articles