C# 在运行时找不到库
我有一个使用 dll 的 C# 项目。 我将 dll 添加到项目引用中,并将属性“Copy Local”设置为 False(我不想拥有该 dll 的本地副本)。
编译看起来不错,但是当我尝试运行 C# 应用程序时,它找不到 dll。
我可以在哪里告诉项目在运行时到哪里寻找库?
谢谢
I have a C# project that uses a dll. I added the dll to project references and I set the property Copy Local to False (I do not want to have local copies of that dll).
The compilation looks fine, but when I try to run the C# application, it cannot find the dll.
Where can I tell the project where to look for the library during runtime?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
应用程序将在与可执行文件相同的路径和路径环境中查找 .dll。
但正如 BtBh 所说:仅当将此组件放入 GAC 中时才使用关闭开关。
The application will look for the .dll in the same path as the executable and in the path-env.
But as BtBh said: only use the off-switch if you put this assembly in the GAC.
为什么将所有外部组件与您制作的组件一起复制是很尴尬的?
您可以使用 XCOPY 部署,以便所有程序集驻留在 1 个位置。
部署程序集的另一种方法是将它们全部打包在一个安装包 (MSI) 中。
Why is it ackward to copy all the external assemblies with the ones you made?
You can use XCOPY deployment so that all assemblies reside in 1 location.
An other way to deploy your assmblies is to package them all in 1 setup package (MSI).
如果您不想将第三方 dll 与您自己的 dll 一起分发,您可以:
1) 说明要求并假设该 dll 将安装在 GAC 中。 它在大多数情况下都不起作用,并且在本地安装程序集有一个目的:如果某些系统范围的更新与您的依赖项混淆,您的应用程序不应该中断。
2) 硬着头皮将第三方 dll 与您自己的 dll 一起分发。
3) 如果这样做是合法的(注意第三方 dll 的许可条款),请使用 IL 合并 静态链接您的程序集(您自己的程序集和任何第三方)。 使用 ILMerge,您最终可以获得包含所有引用的单个程序集。 您仅在打包部署时进行合并(有自定义的 msbuild/NAnt 任务可以为您执行此操作),在 VS 中开发时,您只需继续像以前一样进行(引用程序集)。 ILMerge 在相当多的项目中使用,以提供独立、紧凑的可执行文件(我想到了 LinqPad)。
If you don't want to distribute third party dll's with your own, you can either:
1) State the requirements and assume the dll will be installed in the GAC. It won't work most of the time, and having assemblies locally serves a purpose: your application is not supposed to break if some system wide update messes with your dependencies.
2) Bite the bullet and distribute the third party dll's with your own.
3) Providing it's legal to do so (watch for the licensing provisions of your third party dll's) use IL Merge to statically link your assemblies (your own and any third party). With ILMerge you can end up with having a single assembly containing all your references. You only merge when packaging for deployment (there are custom msbuild/NAnt tasks that do so for you), when developping in VS you just keep doing as you used to (referencing assemblies). ILMerge is used in quite a few projects to have a self-contained, compact executable (LinqPad comes to mind).
请查看这篇 MSDN 文章。 这是关于 元素的。
这允许您告诉应用程序可以在哪里查找除默认 /bin 文件夹之外的程序集。
请注意,它会查找“子目录”,因此它不能是完全不同的文件夹。 它必须驻留在您的应用程序基文件夹中。
Take a look at this MSDN article. It's about the <probing> element.
This allows you to tell the application where it can look for assemblies other than the default /bin folder.
Please note that it looks for 'subdirectories', so it cannot be a totally different folder. It has to reside in your application base folder.
使用 Fusion 日志查看器 追踪问题与解决程序集。
如果您不想拥有 DLL 的本地副本,则必须将其放入全局程序集缓存 (GAC) 中或添加 程序集重定向指令到您的 app.config 或 machine.config。
Use the Fusion Log Viewer to track down problems with resolving assemblies.
If you don't want to have a local copy of the DLL you must put it in the Global Assembly Cache (GAC) or add Assembly Redirection instructions to your app.config or machine.config.
为什么要将项目引用的 copylocal 设置为 false? 不建议这样做。
观察两者之间的区别:
GAC 程序集始终运行时解析。 默认情况下,GAC 程序集是共享程序集(考虑到重用)。
编译时解析用于完成构建。 尽可能使用项目引用(使用解决方案中的程序集)。 当您不负责要使用的那些程序集的构建并且这些程序集不在您的解决方案中时,请使用文件引用。
Why are you setting copylocal to false for project references? It is not recommended to do that.
Watch the difference between:
GAC assemblies are always runtime resolved. GAC assemblies are shared assemblies by default (with reuse in mind).
Compile time resolvement is used to get the build done. Use project references as much as possible (with assemblies in your solution). Use file references when you are not in charge of the build of those assemblies you want to use and those assemblies are not in your solution.
如果将 CopyToLocal 设置为 false,则将该程序集安装在 GAC。
If you set CopyToLocal to false, you to install that assembly in the GAC.
要引用此 dll,需要将其复制到本地或 GAC(全局程序集缓存)中。 拥有本地副本是首选方法,因此我建议将本地副本切换为 true。
To reference this dll it either needs to be copied locally or in the GAC (Global Assembly Cache). Having a local copy is the preferred way to do it, so I would recommend switching copy local to true.