Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I catch a missing dll error during application load in C#?

Tags:

c#

.net

dll

Is it possible to catch the exception when a referenced .dll cannot be found?

For example, I have a C# project with a reference to a third-party dll; if that dll cannot be found, an exception is thrown. The exception is a System.IO.FileNotFoundException, but I am unable to determine where to catch it. The following code did not seem to work:

static class Program {     /// <summary>     /// The main entry point for the application.     /// </summary>     [STAThread]     static void Main()     {         try         {           // code goes here         }         catch (Exception exc)         {             MessageBox.Show(exc.ToString());         }     } } 
like image 347
Richard Morgan Avatar asked Jul 10 '09 18:07

Richard Morgan


People also ask

What happens if DLL files are missing?

dll file or by reinstalling the program, the hardware component, or the driver that is causing the error. If these procedures do not correct the User32. dll error, you can restore your computer to a condition before the error appeared by using the Windows System Restore feature.

What is the easiest fix for a missing DLL error?

Restart your PC The easiest way to fix the missing . dll file error is to restart your PC. Many times, there are cache problems that a restart fixes automatically.

What is the cause of a missing DLL?

This may happen when a program is uninstalled/installed or you have tried to clean up space on the hard disk. A recent application installation sometimes overwrites an existing DLL file with an incompatible or invalid DLL file. A malicious program has deleted or damaged a DLL file.


1 Answers

Extending Josh's answer.

Assemblies in .Net are loaded on demand by the CLR. Typically an assembly load won't be attempted until a method is JIT'd which uses a type from that assembly.

If you can't catch the assembly load failure with a try/catch block in the main method, it's likely beceause you're using a type from the assembly within the try/catch. So the exception occurs before the main method is actually run.

Try putting all of the code from the main method in a different function. Then call that function within the try/catch block and you should see the exception.

like image 88
JaredPar Avatar answered Sep 28 '22 07:09

JaredPar