如果dll文件意外从应用程序目录中删除 [英] if dll file is accidentally deleted from application directory

查看:81
本文介绍了如果dll文件意外从应用程序目录中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果dll文件存在于应用程序目录中,则可以正常工作,但是,如果dll文件被意外删除,则程序运行会崩溃,而不会处理该异常.组装使用的是:

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

推荐答案

当然没有了,应用程序当然会死掉.如果您的类中有一个需要给定dll的using子句,则对应用程序无法解析的名称空间的任何调用都将导致致命错误.
解决方案:确保正确引用了程序集,并且该程序集出现在您的输出目录中.
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.


如果DLL不存在,则无法捕获错误,因为您的应用程序没有抛出错误时不存在.运行.net应用程序时,框架会从清单中提取所需的DLL,并尝试对其进行解析. (请注意,它实际上并没有加载它们……直到JIT编译器需要它们时才发生.)当它无法在GAC或app目录中找到库时,它将抛出异常. .现代操作系统的基本约定之一是所需的库不会被意外删除" ...作为软件开发人员,这是我们对用户的隐性要求.如果他们开始从应用程序目录中删除文件...那么,那么他们就应该得到它们.

如果您有合理的理由在应用程序部署后替换DLL(例如,封装一些可能会更改的功能),则可以创建一个库,该库提供DLL所需的接口和类型并链接应用程序反对那些.然后在运行时使用反射来查找其中具有实际功能的DLL.然后,您可以实例化所需类的具体实例.请注意,DLL也必须链接到接口库.这样,您可以在部署应用程序后替换"一个或多个DLL,而无需强制卸载/重新安装.
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.


有时会发生的事情是您拥有bin目录中还剩下一些dll,应用程序将继续正常运行.删除bin目录,它不再运行.您可能会缺少对所需dll的引用,该引用不是编译应用程序所必需的,而是编译所必需的另一个引用所必需的.
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.


这篇关于如果dll文件意外从应用程序目录中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆