检查系统中是否存在DLL
快速提问。我想查明执行我的应用程序的系统中是否存在 DLL。
这在 C# 中可能吗? (以一种适用于所有 Windows 操作系统的方式?)
对于 DLL,我的意思是非 .NET 经典 DLL(Win32 DLL)
(基本上我想进行检查,因为我使用的 DLL 可能是也可能不是存在于用户系统上,但我不希望应用程序在不存在时崩溃而没有警告:P)
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 Windows OS?)
For DLL i mean a non-.NET classic dll (a Win32 dll)
(Basically I want to make a check cause I'm using a DLL that may or may not be present on the user system, but I don't want the app to crash without warning when this is not present :P)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
调用
LoadLibrary
API函数:Call the
LoadLibrary
API function:在 .NET 中使用平台调用时,您可以使用
Marshal.PrelinkAll(Type)
方法:正如您所看到的,除了 dll 是否存在之外,它还执行其他检查,例如定位入口点(例如
SomeMethod()
和SomeMethod2()
是否实际存在于进程中就像下面的代码一样)。然后使用
try
/catch
策略来执行检查:When using platform invoke calls in .NET, you could use the
Marshal.PrelinkAll(Type)
method:As you can see, it performs additional checks other than if the dll exists, like locating the entry points (e.g if
SomeMethod()
andSomeMethod2()
actually exist in the process like in the following code).Then use a
try
/catch
strategy to perform your check:实际上它不会抛出FileNotFoundException。
此外,还需要在多个位置检查路径,对于 LoadLibrary,
.net 中有一个标准异常,它派生自 TypeLoadException,即 DllNotFoundException。 。
最好的方法是在 try..catch 中包装方法/PInvoke 调用并处理 DllNotFoundException,因为 .net 将检查应用程序路径以及设置为 PATH OS 环境变量一部分的任何其他路径。
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.
调用加载库。
http://msdn.microsoft.com/en-us /library/ms684175(VS.85).aspx
Call LoadLibrary.
http://msdn.microsoft.com/en-us/library/ms684175(VS.85).aspx
我假设这是一个 PInvoke 调用?
如果是这样,确定文件是否存在的最简单方法是进行调用并捕获文件不存在时产生的异常。
I'm assuming this is a PInvoke call?
If so the easiest way to make this determine if it's present is to make the call and catch the exception that results if the file does not exist.