c# - C# Class Library throws FileNotFoundException while trying to load another library
问题描述
I try to connect to a fiscal device with a C#.
I use this documentation to do so: http://integration.atol.ru/api-en/#connection-to-project
So basically I have a driver of the device installed on my PC (fprt10.dll) and there is a "wrapper" assembly that allows me to work with this driver from C# (Atol.Drivers10.Fptr.dll). I import this wrapper into my project as a reference.
I have the following constructor in my class:
public MyClass()
{
IFptr fiscalPrinter = new Fptr();
// Here comes several settings to configure connection
fiscalPrinter.applySingleSettings();
fiscalPrinter.open();
fiscalPrinter.beep();
fiscalPrinter.close();
}
To test the solution I use another application, that loads my Class Library as a dependency. When I call a constructor of MyClass I get an exception:
System.IO.FileNotFoundException: Driver not installed
at Atol.Drivers10.Fptr.Fptr.loadDriver(String path)
at Atol.Drivers10.Fptr.Fptr..ctor()
at MySolution.MyClass.MyClass()
...
If I create instance of Fptr with a path to the driver
IFptr fiscalPrinter = new Fptr(@"C:\path\fptr10.dll")
I get the slightly different exception, but I believe the problem is the same:
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()
...
But when I create a Console Application and put in there exact same code (both versions with path and without), everything works: the device beeps, there are no exceptions.
What could be the reason for that behavior and how to fix this?
解决方案
The issue may be one of the following
- The test application is using 'target platform' different than the console application which works fine. The device driver folders expected for each platform could be different. e.g. Changing the targeted platform from 'any CPU' to 'x64' / 'x86' (depending on the type of OS where you are running it) will help
- Try running the test application from admin command prompt. Permissions issue may reflect as 'file not found' (instead of 'file could not be loaded').
- Use an assembly binding viewer tool to debug the issue further
- Refer to Could not load file or assembly or one of its dependencies for more discussion and inputs on the assembly loading issues.
推荐阅读
- ajax - 如何使用 Angular 中的 URL 编码对象发出获取请求?
- css - 如何为 Bootstrap 4 类添加前缀以避免 css 类冲突
- mongodb - 如何在一个 Elasticsearch 索引中索引两个 mongodb 集合
- c++ - CMake GLOB_RECURSE 未按预期工作
- python - 为服务文件调用的 uwsgi 应用设置 oracle 路径
- linux - Linux 脚本中的模块加载/卸载命令
- r - 使用求和运算符将公式转换为 R
- scala - 与其他字符串和输出数组匹配的字符串
- webpack - 设置 Phoenix 1.4 + Webpack + Zurb Foundation
- flutter - Flutter firebase_messaging 包启用/禁用通知声音和振动