0

如果我的 VB.NET 程序所需的某些 dll 丢失,当我运行它时,它会崩溃并显示典型的 Windows 错误消息,并且不提供有关该错误的任何信息。

所以,我想在做任何事情之前验证所有依赖项都得到满足。但这并不是微不足道的,因为我有非托管和运行时依赖项。那么,在深入研究之前,是否有一些 CLR 设置,或者这个问题的一些更简单的解决方案?

4

1 回答 1

0

这真的很容易(也就是说,避免了 Windows 的样板消息)。您只需让您的应用程序处理UnhandledException事件。

对于像我这样的 VB.NET 应用程序窗体,这意味着转到项目属性、Application选项卡并单击View Application events按钮。

这打开了(默认隐藏)ApplicationEvents.vb 文件。我添加了一些代码:

Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private Sub MyApplication_UnhandledException(ByVal sender As Object, _
                                                     ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs _
                                                     ) Handles Me.UnhandledException
            MessageBox.Show("Application crashed due to the following unhandled exception. " + e.Exception.ToString())
        End Sub

    End Class    

End Namespace

现在,如果我缺少一些 dll,我会在异常消息中获得它的名称(如果它是托管的或混合的)。如果缺少的 dll 是非托管的,我只会得到需要它的混合程序集的名称。但这对我来说没关系;将混合程序集扔到 Depends.exe 上会很快发现有问题的非托管程序集。

于 2012-11-21T14:13:52.940 回答