Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
If dll file exists in the application directory, it works fine, but if the dll file is accidentally deleted, the program run collapses without handling the exception. The assembly is used:
using xxx.dll;
.
.
.
private void dosomething()
{
    if(!System.IO.File.Exists(xxx.dll)
        return;
    try
    {
         //use xxx.dll functions here
    }
    catch(Exception ex)
    {return;}
}
The program can not handle the exception and collapses immediately. Is there away to
Posted 19-Nov-12 9:23am
Edited 19-Nov-12 9:46am
v2
Comments
Sergey Alexandrovich Kryukov at 19-Nov-12 15:34pm
   
What's the problem? Don't delete it accidentally...
"Is there a way to"... -- what? Not a complete question, I guess... :-)
--SA
Albert Holguin at 20-Nov-12 0:40am
   
If you really need this feature, you can do delayed library loading in C++... The DLL is not loaded until your code does so explicitly, giving you the opportunity to check for missing components.... But again, that's C++...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Of course the application dies. If you have a using clause in your class that requires a given dll, then any call made to a namespace the application can't resolve will cause a fatal error.
The solution: Make sure the assembly is properly referenced and that it appears in your output directories.
  Permalink  
Comments
Nelek at 19-Nov-12 17:15pm
   
OP's comment moved from non-solution below
Thanks for the answer,
Of course every thing work fine when lib file exists, but is there any way to catch the error, or to stop the solution from collapsing, if the lib file is accidentally deleted later on?
Nelek at 19-Nov-12 17:17pm
   
OP's comment moved from non-solution below (2)
Thanks guys for the help. I will try to confirm existence of all lib files, if not kill the process.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

There is no way to catch the error if the DLL doesn't exist because your app doesn't exist when the error is thrown. When a .net application is run, the framework extracts the required DLLs from the manifest and attempts to resolve them. (Note that it doesn't actually load them... that doesn't happen until the JIT compiler needs them.) When it can't find a library in the GAC or the app directory, it will throw the exception. One of the fundamental conventions of modern operating systems is that the required libraries won't get "accidentally deleted"... as software developers this is an implicit requirement we make of our users. If they go and start deleting files from the app directory... well, then they deserve what they get.
 
If you have a legitimate reason to replace a DLL after the application has been deployed (for example, to encapsulate some functionality that is likely to change) you can create a library that provides interfaces and types that you need from the DLL and link your application against those. Then use reflection at runtime to locate the DLL that has the actual functionality in it. You can then instantiate concrete instances of the classes you need. Note that the DLL must ALSO be linked to the interface library. In this manner you can do 'in-place' replacement of one or more DLLs after the application has been deployed without forcing an uninstall/reinstall.
  Permalink  
Comments
Nelek at 19-Nov-12 17:17pm
   
OP's comment moved from non-solution below
Thanks guys for the help. I will try to confirm existence of all lib files, if not kill the process.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

What sometimes happens is that you have some dll's left in the bin directory, and the application continues to run fine. The delete the bin directory, and it no longer runs. You are probably missing a reference to a required dll that is not required to compile the application, but required by another reference that is required for compilation.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

You're going about this the wrong way.
 
First, if a .DLL is deleted, the users have too many permissions to the Program Files folder. By default, users cannot modify the contents of anything under Program Files, and hence can't delete a .DLL either, under Vista and above. If they can, someone has given the users the permissions required to do so. This is a support nightmare and should be avoided at all costs.
 
Second, the solution is not with your code, but with your installer. .MSI-based installers can check for the existence of required files when you application is launched and replace them if they are missing. This is the point behind self-healing application support in Windows Installer. If you're installer is written correctly, you don't have to do anything in your code to detect or replace the missing files. Windows Installer will fix them if they are missing.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 420
1 CHill60 410
2 Sergey Alexandrovich Kryukov 384
3 PIEBALDconsult 235
4 BillWoodruff 190


Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100