在 Visual Studio 中编译时如何将 DLL 保存在不同的文件夹中?

发布于 2024-10-19 12:19:55 字数 291 浏览 8 评论 0原文

假设我有一个窗口窗体 / 控制台应用程序 C# 项目,其中包含一些外部引用以及对其他类库的引用项目也在同一解决方案中。

当我构建Window Form项目时,我希望将引用的库存储在不同的位置(例如:bin\Release\Libraries), 并且与 .exe 不在同一文件夹中

可以吗?

Let's suppose I have a Window Forms / Console Application C# project with some external references and references to other class library projects in the same solution too.

When I build the Window Form project, I want the referenced libraries be stored in a different location (eg: bin\Release\Libraries), and not in the same folder as the .exe.

Is it possible to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

萝莉病 2024-10-26 12:19:55

您的问题有两个部分:

如何配置解决方案以将程序集/EXE 构建到您选择的文件夹中 - 这是通过 VS 中项目的属性进行配置的(项目属性 -> 构建 -> 输出路径)。还检查每个引用上的“复制本地”属性的值。

如何从非默认位置(即从 ...\Libraries 文件夹)加载程序集文件 - 您需要更改 app.config 文件以将此非默认路径添加到程序集搜索位置。

链接到 Microsoft 站点不再有效,因此来自回程机器的摘要: 如何在运行时加载位于应用程序 bin 文件夹之外的文件夹中的程序集

方法 1:在全局程序集缓存 (GAC) 中安装程序集。
GAC 是安装公共语言运行时的计算机范围的代码缓存。 GAC 存储您专门指定由多个应用程序共享的程序集。

注意您只能在 GAC 中安装强名称程序集。

方法 2:使用带有标签的应用程序配置 (.config) 文件
.config 文件包含以下设置:

• 特定于应用程序的设置

• 公共语言运行时读取的设置,例如程序集绑定策略设置和远程处理对象设置

• 应用程序读取的设置

标记指定公共语言运行时可以在何处找到程序集。公共语言运行时应用 .config 文件中 标记的设置。 标记的设置确定程序集的版本和位置。

方法 3:使用 AssemblyResolve 事件
每当公共语言运行时尝试绑定到程序集但失败时,AssemblyResolve 事件就会触发。您可以使用 AddHandler 方法将事件处理程序添加到应用程序,每当 AssemblyResolve 事件触发时,该处理程序都会返回正确的程序集。

AssemblyResolve 事件处理程序必须返回一个 [Assembly] 对象,并且公共语言运行时必须绑定到该对象。通常,您可以使用 Assembly.LoadFrom 方法加载程序集,然后返回对象。

There are 2 parts of your question:

How to configure solutions to build assemblies/EXE into folders of your choice - this is configured through properties of the project in VS (project properties -> build -> output path). Also value of check "copy local" property on each reference.

How to load assemblies files from non-default locations (i.e. from your ...\Libraries folder) - you need to make changes to your app.config file to add this non-default paths to assembly search location..

Link to Microsoft site no longer works, so summary from wayback machine: How to load an assembly at runtime that is located in a folder that is not the bin folder of the application:

Method 1: Install the assembly in the global assembly cache (GAC).
The GAC is a computer-wide code cache where the common language runtime is installed. The GAC stores assemblies that you specifically designate to be shared by several applications.

Note You can only install strong-named assemblies in the GAC.

Method 2: Use an application configuration (.config) file with the tags
A .config file contains the following settings:

• Settings that are specific to an application

• Settings that the common language runtime reads, such as the assembly binding policy settings and the remoting objects settings

• Settings that the application reads

The <codeBase> tags specify where the common language runtime can find an assembly. The common language runtime applies the settings of the <codeBase> tags from the .config file. The settings of the <codeBase> tags determine the version and the location of the assembly.

Method 3: Use the AssemblyResolve event
The AssemblyResolve event fires whenever the common language runtime tries to bind to an assembly and fails. You can use the AddHandler method to add an event handler to the application that returns the correct assembly whenever the AssemblyResolve event fires.

The AssemblyResolve event handler must return an [Assembly] object, and the common language runtime must bind to this object. Typically, you can use the Assembly.LoadFrom method to load the assembly and then to return the object.

水中月 2024-10-26 12:19:55

前面已经给出了正确答案。我只想提一下,有一个名为 PrettyBin 的 nuget 包。

将其安装到您的启动项目中。 DLL 和 XML 将转到 lib 文件夹,如果您不想自定义,您将获得一个说明其工作原理的工作示例。

Correct answers were given earlier. I'll just mention that there is a nuget package for this called PrettyBin.

Install it on your startup project. DLLs and XMLs will go to a lib folder and you'll have a working example of how it's done, if you won't to customize.

此岸叶落 2024-10-26 12:19:55

在项目属性中设置引用路径。

您还可以通过在项目对象中指定输出路径来指定编译后的 exe 的去向。

Set Reference path in project peoperties.

You can also specify where your compiled exe goes by specifying Output path in project peoperties.

谎言 2024-10-26 12:19:55

您可以在此处找到组织项目参考的最佳实践:http://codebetter.com/patricksmacchia/2009/01/11/lessons-learned-from-the-nunit-code-base/

查看“VisualStudio 项目参考 + 复制本地 true”一章选择是邪恶的!”

You'll find best practices for organizing project references here: http://codebetter.com/patricksmacchia/2009/01/11/lessons-learned-from-the-nunit-code-base/

Look under chapter "The VisualStudio Project Reference + Copy Local true option is evil!"

◇流星雨 2024-10-26 12:19:55

是的,这是可能的,您可以在 msbuild 脚本中执行此操作。虽然我无法给您确切的答案,但请查看这里的问题 使用 msbuild 复制所有文件和文件夹

Yes it is possible, you'd do it in your msbuild script. While I can't give you an exact answer, look here at this question on SO Copy all files and folders using msbuild

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文