简体   繁体   English

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

[英]How to save DLLs in a different folder when compiling in Visual Studio?

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.假设我有一个Window Forms / Console Application C#项目,其中包含一些外部引用以及对同一解决方案中其他类库项目的引用。

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 .当我构建Window Form项目时,我希望引用的库存储在不同的位置(例如: bin\\Release\\Libraries ),而不是与 .exe 位于同一文件夹中

Is it possible to do?有可能吗?

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).如何配置解决方案以将程序集/EXE 构建到您选择的文件夹中 - 这是通过 VS 中的项目属性配置的(项目属性 -> 构建 -> 输出路径)。 Also value of check "copy local" property on each reference.还检查每个引用的“复制本地”属性的值。

How to load assemblies files from non-default locations (ie from your ...\\Libraries folder) - you need to make changes to your app.config file to add this non-default paths to assembly search location..如何从非默认位置(即从 ...\\Libraries 文件夹)加载程序集文件 - 您需要更改 app.config 文件以将此非默认路径添加到程序集搜索位置..

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 :指向 Microsoft 站点的链接不再有效,因此来自回溯机器的摘要: 如何在运行时加载位于不是应用程序 bin 文件夹的文件夹中的程序集

Method 1: Install the assembly in the global assembly cache (GAC).方法 1:在全局程序集缓存 (GAC) 中安装程序集。 The GAC is a computer-wide code cache where the common language runtime is installed. GAC 是计算机范围的代码缓存,公共语言运行时安装在其中。 The GAC stores assemblies that you specifically designate to be shared by several applications. GAC 存储您专门指定由多个应用程序共享的程序集。

Note You can only install strong-named assemblies in the GAC.注意您只能在 GAC 中安装强名称程序集。

Method 2: Use an application configuration (.config) file with the tags A .config file contains the following settings:方法 2:使用带有标记的应用程序配置 (.config) 文件 .config 文件包含以下设置:

• 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. <codeBase>标记指定公共语言运行时可以在何处找到程序集。 The common language runtime applies the settings of the <codeBase> tags from the .config file.公共语言运行时应用 .config 文件中<codeBase>标记的设置。 The settings of the <codeBase> tags determine the version and the location of the assembly. <codeBase>标签的设置决定了程序集的版本和位置。

Method 3: Use the AssemblyResolve event The AssemblyResolve event fires whenever the common language runtime tries to bind to an assembly and fails.方法 3:使用 AssemblyResolve 事件只要公共语言运行时尝试绑定到程序集并失败,就会触发 AssemblyResolve 事件。 You can use the AddHandler method to add an event handler to the application that returns the correct assembly whenever the AssemblyResolve event fires.您可以使用 AddHandler 方法向应用程序添加一个事件处理程序,该程序在 AssemblyResolve 事件触发时返回正确的程序集。

The AssemblyResolve event handler must return an [Assembly] object, and the common language runtime must bind to this object. AssemblyResolve 事件处理程序必须返回一个 [Assembly] 对象,并且公共语言运行时必须绑定到这个对象。 Typically, you can use the Assembly.LoadFrom method to load the assembly and then to return the object.通常,您可以使用 Assembly.LoadFrom 方法加载程序集,然后返回对象。

Correct answers were given earlier.前面已经给出了正确的答案。 I'll just mention that there is a nuget package for this called PrettyBin .我只想提一下,有一个名为PrettyBin的 nuget 包。

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. DLL 和 XML 将转到 lib 文件夹,如果您不进行自定义,您将获得有关如何完成的工作示例。

Set Reference path in project peoperties.在项目peoperties中设置参考路径。

You can also specify where your compiled exe goes by specifying Output path in project peoperties.您还可以通过在项目 peoperties 中指定输出路径来指定编译的 exe 的位置。

You'll find best practices for organizing project references here: http://codebetter.com/patricksmacchia/2009/01/11/lessons-learned-from-the-nunit-code-base/您可以在此处找到组织项目参考的最佳实践: 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!"查看章节“VisualStudio 项目参考 + 复制本地 true 选项是邪恶的!”

Yes it is possible, you'd do it in your msbuild script.是的,这是可能的,您可以在 msbuild 脚本中进行。 While I can't give you an exact answer, look here at this question on SO Copy all files and folders using msbuild虽然我不能给你一个确切的答案,但请看这里关于 SO Copy all files and folders using msbuild 的这个问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在带有nuget包的Visual Studio中,是否可以在发布时更改.dlls文件夹? - In visual studio with nuget packages is it possible to change the .dlls folder when publishing? 如何在Visual Studio 2017解决方案中设置文件夹以在其中搜索DLL? - How to set folder in a visual studio 2017 solution for searching DLLs there? 如何在Visual Studio 2010中管理支持DLL的不同版本? - How to manage different releases of supporting DLLs in Visual Studio 2010? 如何使用两个不同版本的 Visual Studio 处理 TFS DLL - How to handle TFS DLLs with two different versions of Visual Studio 如何将选定的 nuget 包中的 dll 复制到 Visual Studio 中的输出文件夹? - How to copy dlls from selected nuget packages to output folder in Visual Studio? 是否可以在Visual Studio 2008中为具有相对路径的DLL设置共享文件夹? - Is it possible to setup a shared folder for DLLs with relative pathing in Visual Studio 2008? Visual Studio 2017-发行文件夹中可能不需要的dll - Visual Studio 2017 - Potentially unneeded dlls in release folder 是否可以在Visual Studio中更改引用(dll)默认生成文件夹? - Is possible to change references (dlls) default build folder in visual studio? Visual Studio Code:如何配置“dotnet”在编译时不显示警告 - Visual Studio Code: How to configure 'dotnet' not to show warnings when compiling 在Visual Studio中构建时/ bin中不产生DLL / EXE - No DLLs/EXEs produced in the /bin when building in Visual Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM