Assembly.Load to not throw a FileNotFoundException

I'm trying to have Assembly.Load or LoadFrom not throw the FileNotFoundException if the assembly does not exist. Return a null maybe if possible.

Using Path.FileExists is out of the question since the assembly can be in the GAC or even in HTTP (as part of a no touch deployment scenario) and I want the checking procedure to be as fast as possible.

Any help is appreciated.

Thanks,
Kousay< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />



Answer this question

Assembly.Load to not throw a FileNotFoundException

  • Olaf Neuendorf

    GetModuleHandle only looks for libraries already loaded into the process. If it returns NULL it doesn't mean that a file doesn't exist, only that it hasn't been loaded.

     



  • kavita bhalodia

    Ah yes I forgot, thanks for that Smile.

    -chris

  • ahuddy

    Just a quick question. Why are your users debugging your application

  • Gramm

     ivolved wrote:
    Just a quick question. Why are your users debugging your application


    The DLL is mine, the EXE is theirs.

    Thanks to all again.  I ended up adding an interface to control what plug-ins to load and where from in a strict manner.  Users who do not want to see any exceptions when debugging will have to use that.

  • Temple

    Hi,

    public bool LoadAssemblyByName(string strAsmName) {
    try {
           Assembly.Load(strAsmName);
           return true;
    } catch(FileNotFountException ex) {
          return false;
    }

    HTH,



  • akks

    Thank you all,

    This code is in a dll that is part of a “plug-ins” search and load code.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    It is working fine with the try/catch(FileNotFoundException), however, when my users have the “Debug/Exceptions/Common Language Runtime Exceptions” checked to “Break into the debugger” even if the exception is handled, it gets annoying.  The compiler will break on every missing assembly.  I'm trying to find a way around that.

     

    I wish there is a way to gracefully check if the assembly exists and if does not, simply not even trying to load.

     

    The best I could come up with is using File.Exists and searching the usual paths.  Problem with this is searching the GAC is very slow (and not recommended) let alone the case where the app is hosted in HTTP.

     

    Another approach is to load into a separate AppDomain, but that is also terribly slow.

     

    I tried looking into something like AppDomain.AssemblyResolve and AssemblyLoad events.  Neither helps in my situation.

     

    Checking for assembly existence should be a very common operation, no   I do not see why do we have to rely on try/catch as logic in this case.

     

    Any ideas

     

    Thanks,

    Kousay


  • vispper


    Probably you could try using GetModuleHandle function, as it can search through the GAC as well, and it seems faster than manual search, but for some reason it misses some entries from the GAC if there are multiple versions of the assemblies (however, its not always the case):



    [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
    internal static extern IntPtr GetModuleHandle (string lpModuleName);

     


    Here's how you call it:




       IntPtr ptr = GetModuleHandle("mscorlib.dll");

       if (ptr == IntPtr.Zero)
       {
          // File or assembly does not exist...
       }
       else
       {
          // do your stuff here...
       }


     


    But the problem with that solution is, it only can check through local files, not HTTP...

    Hope this adds to your alternative options,

    -chris



  • Len Miller

    Or better yet catch only FileNotFoundException.

     

     



  • kathy79

    What would the purpose behind your question be What's wrong with just catching the FileNotFoundException Because ultimately, you're going to be duplicating the logic in the Load/LoadFrom methods in order to determine that the file *isn't* really there. And that technique is prone to errors, not to mention maintenance if MS changes the logic in the future.

    Hope that helps.



  • 10e

    You can wrap your Assembly.Load call to a bindless try/catch block like this:



    try
    {
     Assembly.Load("not_existing_assembly.dll");
    }
    catch{}

     


    Regards,

    -chris

  • Assembly.Load to not throw a FileNotFoundException