我有一个.NET 2.0程序,它具有对Interop.WIA.dll(.NET)的引用,它需要在系统中注册库wiaaut.dll。如何避免.net程序在dll未注册时启动崩溃?
如果我的程序运行在未注册wiaaut.dll的操作系统(例如新的Windows XP安装),则该程序在启动时崩溃。
我已经在try/catch块中包含了main中的所有代码,但没有引发异常。有什么办法可以解决这个问题吗?
我有一个.NET 2.0程序,它具有对Interop.WIA.dll(.NET)的引用,它需要在系统中注册库wiaaut.dll。如何避免.net程序在dll未注册时启动崩溃?
如果我的程序运行在未注册wiaaut.dll的操作系统(例如新的Windows XP安装),则该程序在启动时崩溃。
我已经在try/catch块中包含了main中的所有代码,但没有引发异常。有什么办法可以解决这个问题吗?
如果包含您的main方法的类的主要方法,或静态字段,从缺少DLL引用类型,异常将被抛出时在执行主方法中的任何代码之前进行JITting。
最好的解决方案是将所有引用问题的DLL移动到不同的类。通过这种方式,引用不需要JIT你的主要方法,并且你的try/catch可以工作。
喜欢的东西:
class Program
{
public static void Main()
{
try
{
MyClass.AccessMissingDll();
}
catch(...)
{
...
}
}
}
class MyClass
{
public static AccessMissingDll()
{
... access types in your missing DLL here
}
}
您可以尝试捕获并处理AppDomain.UnhandledException
或Application.DispatcherUnhandledException
事件。
AppDomain.UnhandledException
http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx
Application.DispatcherUnhandledException
http://msdn.microsoft.com/en-us/library/system.windows.application.dispatcherunhandledexception.aspx
你可以在这样的WPF
应用程序添加事件。
protected override void OnStartup(StartupEventArgs e)
{
this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
base.OnStartup(e);
}
void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
// Generate Error message...
// Prevent default unhandled exception processing
e.Handled = true;
}
Dispatcher.UnhandledException
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.unhandledexception.aspx