C# Class 库在尝试加载另一个库时抛出 FileNotFoundException
C# Class Library throws FileNotFoundException while trying to load another library
我尝试使用 C# 连接到财务设备。
我使用此文档来执行此操作:http://integration.atol.ru/api-en/#connection-to-project
所以基本上我在我的 PC 上安装了一个设备驱动程序 (fprt10.dll) 并且有一个“包装器”程序集允许我从 C# (Atol.Drivers10.Fptr.dll).我将这个包装器作为参考导入到我的项目中。
我的 class 中有以下构造函数:
public MyClass()
{
IFptr fiscalPrinter = new Fptr();
// Here comes several settings to configure connection
fiscalPrinter.applySingleSettings();
fiscalPrinter.open();
fiscalPrinter.beep();
fiscalPrinter.close();
}
为了测试解决方案,我使用了另一个应用程序,它将我的 Class 库加载为依赖项。
当我调用 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()
...
但是当我创建一个控制台应用程序并在其中放入完全相同的代码(带路径和不带路径的版本)时,一切正常:设备发出哔哔声,没有例外。
这种行为的原因可能是什么以及如何解决这个问题?
问题可能是以下之一
- 测试应用程序使用 'target platform' 与工作正常的控制台应用程序不同。每个平台预期的设备驱动程序文件夹可能不同。例如将目标平台从 'any CPU' 更改为 'x64' / 'x86'(取决于 OS 的类型 运行 它)将有助于
- 尝试 运行 从管理员命令提示符测试应用程序。权限问题可能反映为 'file not found'(而不是 'file could not be loaded')。
- 使用程序集绑定查看器工具进一步调试问题
- 有关程序集加载问题的更多讨论和意见,请参阅 Could not load file or assembly or one of its dependencies。
谢谢samiksc。
问题出在测试应用中。我用的驱动和OS都是x64的,但是测试应用是x86的。使用 x86 驱动程序一切正常。
我尝试使用 C# 连接到财务设备。
我使用此文档来执行此操作:http://integration.atol.ru/api-en/#connection-to-project
所以基本上我在我的 PC 上安装了一个设备驱动程序 (fprt10.dll) 并且有一个“包装器”程序集允许我从 C# (Atol.Drivers10.Fptr.dll).我将这个包装器作为参考导入到我的项目中。
我的 class 中有以下构造函数:
public MyClass()
{
IFptr fiscalPrinter = new Fptr();
// Here comes several settings to configure connection
fiscalPrinter.applySingleSettings();
fiscalPrinter.open();
fiscalPrinter.beep();
fiscalPrinter.close();
}
为了测试解决方案,我使用了另一个应用程序,它将我的 Class 库加载为依赖项。 当我调用 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()
...
但是当我创建一个控制台应用程序并在其中放入完全相同的代码(带路径和不带路径的版本)时,一切正常:设备发出哔哔声,没有例外。
这种行为的原因可能是什么以及如何解决这个问题?
问题可能是以下之一
- 测试应用程序使用 'target platform' 与工作正常的控制台应用程序不同。每个平台预期的设备驱动程序文件夹可能不同。例如将目标平台从 'any CPU' 更改为 'x64' / 'x86'(取决于 OS 的类型 运行 它)将有助于
- 尝试 运行 从管理员命令提示符测试应用程序。权限问题可能反映为 'file not found'(而不是 'file could not be loaded')。
- 使用程序集绑定查看器工具进一步调试问题
- 有关程序集加载问题的更多讨论和意见,请参阅 Could not load file or assembly or one of its dependencies。
谢谢samiksc。
问题出在测试应用中。我用的驱动和OS都是x64的,但是测试应用是x86的。使用 x86 驱动程序一切正常。