Why my .NET App does not load an assembly located in a folder added to the PATH environment variable?
I have a .Net application which uses a couple of .Net dll's, these dll's are located in a folder 开发者_高级运维which is included in the PATH environment variable, but when I start my .Net App if fails with the error:
Could not load file or assembly 'FxDoc.dll, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d4261664821' or one of its dependencies. The system cannot find the file specified.
I already have read these MSDN entries Search Path Used by Windows to Locate a DLL
and Dynamic-Link Library Search Order
Which states:
Windows then searches for the DLLs in the following sequence:
The directory where the executable module for the current process is located.
The current directory.
The Windows system directory. The GetSystemDirectory function retrieves the path of this directory.
The Windows directory. The GetWindowsDirectory function retrieves the path of this directory.
The directories listed in the PATH environment variable.
My questions are:
- What factors could be causing this error?
- Is the above information valid for .Net?
The rules for .NET assemblies and plain-old DLLs are not the same. The rules you listed are for plain-old DLLs.
Assembly loading is surprisingly complicated, but the basic search order is like this:
- Global Assembly Cache (GAC)
- A long list of other locations (including the application base directory, and various subdirectories depending on culture, etc...)
The environment variable PATH isn't used for .NET assemblies. For all the gruesome details, you'll probably want to look at the official documentation:
http://msdn.microsoft.com/en-us/library/aa720133.aspx
Most places I've worked, people stick to the basics -- either the assembly goes into the GAC, or it's placed in the same directory as the application.
You might also be interested in this tool (fuslogvw.exe) which helps you figure out why the loader isn't finding your assembly:
http://msdn.microsoft.com/en-us/library/e74a18c4.aspx
There are several factors that could be at play, including the (relatively new) registry parameter "SafeDllSearchMode". You can get full details here:
http://msdn.microsoft.com/en-us/library/ms682586%28v=vs.85%29.aspx
I would check this link and check for things like your environment's GetAssembly().Location, and CurrentDomain.BaseDirectory:
How do I get the path of the assembly the code is in?
This is also a very good link:
http://blogs.msdn.com/b/aymans/archive/2006/04/04/568466.aspx
Finally, it's possible that maybe the root cause of the error isn't your .dll itself, but some OTHER .dll it depends on. It couldn't hurt to look at your .exe's and .dll's with MS Dependency Walker
For troubleshoouting assembly load failures - http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57120.aspx
CLR does not use PATH to search for assemblies - your links are for native images loading. Check out this one: http://msdn.microsoft.com/en-us/library/yx7xezcf(v=VS.100).aspx
精彩评论