问题
I use [Dllimport("DllName.dll")] where I'm sure a path to my dll exists in the process PATH environment variable, and still I get "DllName.dll could not be found"
回答1:
"DllName.dll could not be found" could also mean that DllImport has not found one of DllName.dll dependencies.
Grab Dependecy Walker to check which dependecy you are missing.
回答2:
I'd suggest you to use FileMon (sysinternals.com) to check if your assumptions about the library location are right.
回答3:
DllImport does not consult the PATH environment variable when loading DLL's. I am fairly certain that DllImport follows the standard rules for loading a native DLL in a native program when the DLL is referenced by name. Here is an article which details those rules
http://msdn.microsoft.com/en-us/library/ms682586.aspx
I've found the simplest way to load the DLL is just to put it in the same location as the program I am executing.
回答4:
I used  const string lcms2Path = "C:\\lcms2.dll"; in my C# application for the longest time. But finally moved the DLL to the BIN folder and this works perfect [DllImport("lcms2.dll")].
来源:https://stackoverflow.com/questions/294080/dllimport-failed-to-locate-dll-even-though-it-is-in-the-path