找不到dll时如何捕获异常?

问题描述 投票:0回答:1

我正在创建一个 Windows 窗体应用程序。这是应用程序的简单结构:

public partial class Form1 : Form
{
    dllLibrary dll_object = new dllLibrary();

    public Form1()
    {
        InitializeComponent(); 
        dll_object.Init();
        dll_object.method1(); 
        this.FormClosing += Form1_FormClosing;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        dll_object.method2();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        dll_object.method3();
    }

    private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
    {
        dll_object.Finalize();
    }
}

这只是我如何创建库的对象并在单击按钮时通过使用该对象来使用库的方法的示例。为了对一个类的不同方法使用同一个对象,我在任何方法之外创建它。

在我将 dll 放入我的 PC 上的文件夹之前,通过命令提示符注册它并在解决方案资源管理器中添加对它的引用。

但是,如果从文件夹中删除了dll,例如,就会出现异常。我希望用户看到未找到 dll 的消息。可以的话怎么办?我不能把 'dllLibrary object = new dllLibrary();' try 语句中的一行以捕获异常,然后显示一个消息框。我不能将这一行放在类的任何方法中,因为我必须为同一个对象运行库的方法。

我读过很多文章,但在这篇文章中,dll 只是在方法内部调用。

c# .net
1个回答
0
投票

据我了解,您想通知用户如果动态链接库 (DLL) 不存在则无法找到它。

就个人而言,我会为该程序制作一个单独的启动器。它不导入任何库。

启动器会先检查是否所有文件(DLL)都存在,如果至少有一个动态链接库被删除或重命名,则报错并且不启动程序,否则EXE应该启动。

还没有测试过,但这是一个例子。

using System;
using System.IO;
using System.Diagnostics;

namespace ProgramLauncher
{
    public class Program
    {
        public static void Main()
        {
            // Using console here. It can be Windows Forms as well.
            
            string EXEPathToLaunch = Environment.ExpandEnvironmentVariables("%PROGRAMFILES%\\MyEXE\\ProgramThatShouldLaunch.exe");
            
            if (!File.Exists("DLL.dll") || !File.Exists("DLL2.dll"))
            {
                // These are 2 dynamic link libraries that would be imported to the program that you would like to launch.
                // You can replace those DLL filenames with anything else.
                // You can check as more DLLs as your program uses.
                
                // Here we would notify the user that one or two of specified dynamic link libraries do not exist.
                
                Console.WriteLine("DLL does not exist! Cannot launch!"); // You can replace this with a Message Box if the launcher is also Windows Forms.
                Environment.Exit(1); // The Console Application will close.
            } else {
                // Run the program if all DLLs are present and not deleted or renamed
                // We can also check if the EXE exists or not.
                if (!File.Exists(EXEPathToLaunch))
                {
                    Console.WriteLine("EXE does not exist! Cannot launch!"); // You can also replace this with a Message Box if the launcher is also Windows Forms.
                    Environment.Exit(1);
                } else {
                    Process.Start(EXEPathToLaunch); // Starts the needed EXE if the EXE itself and other DLLs are found. That way, there's no need to worry if a DLL does not exist.
                    Environment.Exit(0);
                }
            }
        }
    }
}

此外,“object”是一个类似于 var 的关键字。我不建议将它用作变量名。

© www.soinside.com 2019 - 2024. All rights reserved.