我有一个使用几个 .Net dll 的 .Net 应用程序,这些 dll 位于 PATH 环境变量中包含的文件夹中,但是当我启动我的 .Net 应用程序时如果失败并出现错误:
无法加载文件或程序集“FxDoc.dll,Version=1.2.10.0,Culture=neutral,PublicKeyToken=1b44e1d4261664821”或其依赖项之一。系统找不到指定的文件。
我已经阅读了这些 MSDN 条目 Search Path Used by Windows to Locate a DLL
和 Dynamic-Link Library Search Order
其中指出:
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.
我的问题是:
- 哪些因素可能导致此错误?
- 以上信息对 .Net 有效吗?
最佳答案
.NET 程序集和普通旧式 DLL 的规则不同。您列出的规则适用于普通的旧 DLL。
程序集加载出奇的复杂,但是基本的搜索顺序是这样的:
- 全局程序集缓存 (GAC)
- 一长串其他位置(包括应用程序基目录,以及取决于文化的各种子目录等...)
环境变量 PATH 不用于 .NET 程序集。对于所有可怕的细节,您可能需要查看官方文档:
http://msdn.microsoft.com/en-us/library/aa720133.aspx
在我工作过的大多数地方,人们都坚持基本原则——要么将程序集放入 GAC,要么将其放置在与应用程序相同的目录中。
您可能还对这个工具 (fuslogvw.exe) 感兴趣,它可以帮助您找出加载器找不到您的程序集的原因:
关于c# - 为什么我的 .NET 应用程序不加载位于添加到 PATH 环境变量的文件夹中的程序集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7342052/