Check if a DLL is present in the system

前端 未结 5 562
深忆病人
深忆病人 2020-12-06 16:41

quick question. I want to find out if a DLL is present in the system where my application is executing.

Is this possible in C#? (in a way that would work on ALL Wind

5条回答
  •  天涯浪人
    2020-12-06 17:23

    Actually it does not throw FileNotFoundException.

    Also for that one needs to check in multiple places for path, for the LoadLibrary

    There is a standard exception in .net the is derived from TypeLoadException, that is DllNotFoundException.

    Best way is to wrap a method/PInvoke call in try..catch and handle the DllNotFoundException since .net will check for application path as well as any other paths set as part of PATH OS Environment variable.

    [DllImport("some.dll")]
    private static void SomeMethod();
    
    public static void SomeMethodWrapper() {
    try {
          SomeMethod();
        } catch (DllNotFoundException) {
        // Handle your logic here
      }
    }
    

提交回复
热议问题