我可以在 C# 中的应用程序加载期间捕获丢失的 dll 错误吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1111264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I catch a missing dll error during application load in C#?
提问by Richard Morgan
Is it possible to catch the exception when a referenced .dll cannot be found?
当找不到引用的 .dll 时,是否可以捕获异常?
For example, I have a C# project with a reference to a third-party dll; if that dll cannot be found, an exception is thrown. The exception is a System.IO.FileNotFoundException, but I am unable to determine where to catch it. The following code did not seem to work:
例如,我有一个 C# 项目,其中引用了第三方 dll;如果找不到该 dll,则会引发异常。异常是 System.IO.FileNotFoundException,但我无法确定在哪里捕获它。以下代码似乎不起作用:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
// code goes here
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
}
}
采纳答案by JaredPar
Extending Josh's answer.
扩展乔希的回答。
Assemblies in .Net are loaded on demand by the CLR. Typically an assembly load won't be attempted until a method is JIT'd which uses a type from that assembly.
.Net 中的程序集由 CLR 按需加载。通常,在使用该程序集中的类型的方法被 JIT 之前,不会尝试加载程序集。
If you can't catch the assembly load failure with a try/catch block in the main method, it's likely beceause you're using a type from the assembly within the try/catch. So the exception occurs before the main method is actually run.
如果您无法通过 main 方法中的 try/catch 块捕获程序集加载失败,则可能是因为您在 try/catch 中使用了程序集中的类型。所以异常发生在 main 方法实际运行之前。
Try putting all of the code from the main method in a different function. Then call that function within the try/catch block and you should see the exception.
尝试将 main 方法中的所有代码放在不同的函数中。然后在 try/catch 块中调用该函数,您应该会看到异常。
回答by JoshBerke
You can use AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
您可以使用 AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
}
To manually find the assembly if it can't find it automaticaly.
如果无法自动找到程序集,则手动查找程序集。
回答by TinyRacoon
For a Form Application put the try/catch in Program.cs. (Expanding on JaredPat's answer).
对于表单应用程序,将 try/catch 放在 Program.cs 中。(扩展 JaredPat 的回答)。
Like this:
像这样:
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
throw;
}
}
This will catch any missing DLL exceptions from your application. And they'll look similar to this:
这将从您的应用程序中捕获任何丢失的 DLL 异常。它们看起来像这样:
Could not load file or assembly 'TheGreatestDLLEver.Tribute, Version=0.0.0.2, Culture=neutral, PublicKeyToken=jamtoastbutter' or one of its dependencies. The system cannot find the file specified.
无法加载文件或程序集“TheGreatestDLLEver.Tribute, Version=0.0.0.2, Culture=neutral, PublicKeyToken=jamtoastbutter”或其依赖项之一。该系统找不到指定的文件。