如何在C#中缺少dll时跳过产生错误的代码?

时间:2014-04-28 11:10:52

标签: c# dll console

我在C#中有一个控制台应用程序和一个名为AppManager.cs的类库。此类的方法在console Application中使用,如下所示。

         try
         {
            AppManager mgr = new AppManager(); //Want to skip this line when dll is missing.
            mgr.Method_Name(this, true);       //Want to skip this line when dll is missing.
         }
         catch (Exception)
         {
         }

当我发布代码并且仅提取exe时,应用程序无法运行[我知道,因为exe试图找到dll中存在的dll和方法不可用]。

Now my Question is That Is there any way to skip the code which will produce error when it will not find the reference of dll.

我也试过了,但它没有奏效:

        String file = null;
        String filePath = Path.GetDirectoryName(Application.ExecutablePath);
        file = Directory.GetFiles(filePath, "myLibrary.dll", SearchOption.AllDirectories)
               .FirstOrDefault();
        if (file != null)
        {
            AppManager mgr = new AppManager();
            mgr.Method_Name(this, true);
        }

2 个答案:

答案 0 :(得分:0)

如果你这样做,它也不会给出所需的输出。为什么不将dll与exe集成?您可以使用此tool

来完成此操作

答案 1 :(得分:-1)

你能不能简单地检查dll是否存在?

string filePath = @"SOME_PATH";
var exists = File.Exists(filePath);
if(exists)
{
    AppManager mgr = new AppManager();
    mgr.Method_Name(this, true);   
}