【问题标题】:C# Class Library throws FileNotFoundException while trying to load another libraryC# 类库在尝试加载另一个库时抛出 FileNotFoundException
【发布时间】:2020-12-10 21:33:02
【问题描述】:

我尝试使用 C# 连接到财务设备。
我使用此文档来执行此操作:http://integration.atol.ru/api-en/#connection-to-project
所以基本上我的 PC 上安装了设备驱动程序 (fprt10.dll),并且有一个“包装”程序集允许我从 C# (Atol.Drivers10.Fptr.dll) 使用这个驱动程序。我将此包装器作为参考导入到我的项目中。
我的班级中有以下构造函数:

        public MyClass()
        {
            IFptr fiscalPrinter = new Fptr();
            // Here comes several settings to configure connection
            fiscalPrinter.applySingleSettings();
            fiscalPrinter.open();
            fiscalPrinter.beep();
            fiscalPrinter.close();
        }

为了测试解决方案,我使用了另一个应用程序,它将我的类库作为依赖项加载。 当我调用 MyClass 的构造函数时,出现异常:

System.IO.FileNotFoundException: Driver not installed
   at Atol.Drivers10.Fptr.Fptr.loadDriver(String path)
   at Atol.Drivers10.Fptr.Fptr..ctor()
   at MySolution.MyClass.MyClass()
   ...

如果我创建带有驱动程序路径的 Fptr 实例

IFptr fiscalPrinter = new Fptr(@"C:\path\fptr10.dll")

我得到了稍微不同的异常,但我相信问题是一样的:

System.IO.FileNotFoundException: Can`t load driver library "C:\path\fptr10.dll"
   at Atol.Drivers10.Fptr.Fptr.raiseNotFoundError(String path, Exception reason)
   at Atol.Drivers10.Fptr.Fptr.loadDriver(String path)
   at Atol.Drivers10.Fptr.Fptr..ctor(String libraryPath)
   at MySolution.MyClass.MyClass()
   ...

但是当我创建一个控制台应用程序并在那里输入完全相同的代码(带路径和不带路径的两个版本)时,一切正常:设备发出哔哔声,没有例外。
这种行为的原因可能是什么以及如何解决这个问题?

【问题讨论】:

    标签: c# dependencies filenotfoundexception


    【解决方案1】:

    问题可能是以下之一

    • 测试应用程序使用的“目标平台”与运行正常的控制台应用程序不同。每个平台预期的设备驱动程序文件夹可能不同。例如将目标平台从“任何 CPU”更改为“x64”/“x86”(取决于您运行它的操作系统类型)会有所帮助
    • 尝试从管理员命令提示符运行测试应用程序。权限问题可能反映为“找不到文件”(而不是“无法加载文件”)。
    • 使用程序集绑定查看器工具进一步调试问题
    • 请参阅Could not load file or assembly or one of its dependencies,了解有关程序集加载问题的更多讨论和意见。

    【讨论】:

    • 感谢您的详细解答。我尝试了前两个选项,但它们没有帮助。驱动程序肯定是 x64,而测试应用程序是 x86。这可能是问题的根源。我将进一步调查并尝试使用程序集绑定查看器。
    【解决方案2】:

    谢谢samiksc

    问题出在测试应用中。我使用的驱动程序和操作系统都是 x64,但测试应用程序是 x86。使用 x86 驱动程序一切正常。

    【讨论】:

      猜你喜欢
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多