首页 > 解决方案 > 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?

标签: c#dependenciesfilenotfoundexception

解决方案


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.

推荐阅读