从 GAC 加载使用 DLL 的运行代码时防止 System.IO.FileNotFoundException

发布于 2024-08-22 14:40:46 字数 348 浏览 5 评论 0原文

我正在使用第三方 API 来访问一些数据采集硬件(带有 DAQmx 驱动程序的 National Instruments 硬件)。为此,我添加了对其驱动程序 dll 的引用。

当我在安装了驱动程序的机器上运行代码时,没有问题。但是,当我在没有驱动程序的计算机上运行时,我收到无法在 try/catch 中捕获的 System.IO.FileNotFoundException

在执行 API 代码之前,如何检查 dll 是否可用并且其类型是否可以使用。这很重要,因为并非所有机器都支持这种数据采集硬件(因此安装了驱动程序)。

我不确定,但我认为该 dll 已在安装了驱动程序的计算机上的 GAC 中注册。

I'm using a third party API to access some data acquisition hardare (National Instruments hardware with DAQmx driver). To do so, I add a reference to its driver dll.

When I run the code on a machine that has the driver installed, no problem. But when I run on a machine without the driver, I get a System.IO.FileNotFoundException that cannot be caught in a try/catch.

How can I check, before I execute API code, if the dll is available and its types can be used. This is important because not all machines will support this kind of data acquisition hardware (and thus have the driver installed).

I'm not sure, but I think the dll is registered in the GAC on the machines that have the driver installed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九命猫 2024-08-29 14:40:46

您需要执行以下操作。如果程序集存在,只需将其更改为返回真/假。 (摘自此处

static void Main(string[] args)
    {
        AssemblyLoader("System.Xml, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", false);
        AssemblyLoader("System.Xml", false);
        AssemblyLoader("System.Drawing", true);
    }

    public static void AssemblyLoader(string LoadedAssemblyName, bool PartialName)
    {
        try
        {
            Assembly LoadedAssembly;
            Console.WriteLine("| Loading Assembly {0}", LoadedAssemblyName);
            if(PartialName == true)
                LoadedAssembly = Assembly.LoadWithPartialName(LoadedAssemblyName);
            else
                LoadedAssembly = Assembly.Load(LoadedAssemblyName);

            Console.WriteLine("Full Name: {0}", LoadedAssembly.FullName);
            Console.WriteLine("Location: {0}", LoadedAssembly.Location);
            Console.WriteLine("Code Base: {0}", LoadedAssembly.CodeBase);
            Console.WriteLine("Escaped Code Base: {0}", LoadedAssembly.EscapedCodeBase);
            Console.WriteLine("Loaded from GAC: {0}", LoadedAssembly.GlobalAssemblyCache);
        } catch(FileNotFoundException) {
            Console.WriteLine("EXCEPTION: Cannot load assembly.");
        }
    }

You'll want to do something like the following. Just change it to return a true/false if the assembly exists. (taken from here)

static void Main(string[] args)
    {
        AssemblyLoader("System.Xml, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", false);
        AssemblyLoader("System.Xml", false);
        AssemblyLoader("System.Drawing", true);
    }

    public static void AssemblyLoader(string LoadedAssemblyName, bool PartialName)
    {
        try
        {
            Assembly LoadedAssembly;
            Console.WriteLine("| Loading Assembly {0}", LoadedAssemblyName);
            if(PartialName == true)
                LoadedAssembly = Assembly.LoadWithPartialName(LoadedAssemblyName);
            else
                LoadedAssembly = Assembly.Load(LoadedAssemblyName);

            Console.WriteLine("Full Name: {0}", LoadedAssembly.FullName);
            Console.WriteLine("Location: {0}", LoadedAssembly.Location);
            Console.WriteLine("Code Base: {0}", LoadedAssembly.CodeBase);
            Console.WriteLine("Escaped Code Base: {0}", LoadedAssembly.EscapedCodeBase);
            Console.WriteLine("Loaded from GAC: {0}", LoadedAssembly.GlobalAssemblyCache);
        } catch(FileNotFoundException) {
            Console.WriteLine("EXCEPTION: Cannot load assembly.");
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文