C# 如何让 Visual Studio 在构建过程中解析并包含我的项目的所有依赖项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17478968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make Visual Studio resolve and include all the dependencies of my project during build?
提问by Zoltán Tamási
I have a large solution currently under VS2010, with lots of projects and dependencies. Some of them are installed to the GAC, some of them are just included from a "lib" folder. I need to build one of my projects (specifically a WinForms app) to able to run on any, non-development computers without any installation process (except for the .NET runtime of course), just as portable apps do.
我目前在 VS2010 下有一个大型解决方案,有很多项目和依赖项。其中一些已安装到 GAC,其中一些仅包含在“lib”文件夹中。我需要构建我的一个项目(特别是 WinForms 应用程序),以便能够在没有任何安装过程的任何非开发计算机上运行(当然,.NET 运行时除外),就像便携式应用程序一样。
For this to work, I need to have all of the referenced DLLs and their whole dependency tree in the output folder of my EXE. I can do it for exemple by marking the dependencies to "Copy local" in the properties window, but that works only for the direct references of the EXE project, so it's far not enough. Another way is to make a setup project, but my client and also I want to avoid that (in the final version I'm gonna use ClickOnce). Of course I can always do it purely by hand, gathering all the DLLs manually, but that's quite a nightmare.
为此,我需要在 EXE 的输出文件夹中包含所有引用的 DLL 及其整个依赖关系树。例如,我可以通过在属性窗口中将依赖项标记为“复制本地”来实现,但这仅适用于 EXE 项目的直接引用,因此还远远不够。另一种方法是创建一个安装项目,但我的客户和我也想避免这种情况(在最终版本中我将使用 ClickOnce)。当然,我总是可以完全手工完成,手动收集所有 DLL,但这简直是一场噩梦。
Is there some tool, msbuild trick, command-line option, whatever hack to force Visual Studio to gather the whole dependency tree of my EXE during build, and copy them to the output folder? So that I could just ZIP everything together and send to my client.
是否有一些工具、msbuild 技巧、命令行选项,以及强制 Visual Studio 在构建期间收集我的 EXE 的整个依赖树,并将它们复制到输出文件夹的任何技巧?这样我就可以将所有内容压缩在一起并发送给我的客户。
采纳答案by Zoltán Tamási
I actually chose a somewhat "middle" solution, the following way.
我实际上选择了一个有点“中间”的解决方案,如下所示。
- I created a "dummy" setup project, not caring about setting anything but adding the project outputs (primary output, localized resources, contents, etc.). It was a 2 minute task this way.
- I built the setup project, and got the MSI file as the output.
I've extracted the contents of the MSI file to a specific folder, called "MyAppPortable" for example. I found the solution here. The command-line command is
msiexec /a "absolute_path_to_my_MSI_output" /qb TARGETDIR="absolute_path_to_my_desired_output_folder"
I got the full application with all of its resolved dependencies (except for late-binding dependencies, but I took care of them manually, by adding them as references to my projects). I could ZIP the whole folder, put it on another computer, and voila, everything worked fine.
- 我创建了一个“虚拟”设置项目,不关心设置任何东西,只关心添加项目输出(主要输出、本地化资源、内容等)。这是一个 2 分钟的任务。
- 我构建了安装项目,并获得了 MSI 文件作为输出。
我已将 MSI 文件的内容提取到一个特定的文件夹中,例如名为“MyAppPortable”。我在这里找到了解决方案。命令行命令是
msiexec /a "absolute_path_to_my_MSI_output" /qb TARGETDIR="absolute_path_to_my_desired_output_folder"
我得到了完整的应用程序及其所有已解析的依赖项(后期绑定依赖项除外,但我通过将它们添加为我的项目的引用来手动处理它们)。我可以压缩整个文件夹,把它放在另一台电脑上,瞧,一切正常。
回答by Yair Nevet
Basically, using Visual Studio, you can set all of your Solution's Projects to build into the same Output folder and use this folder as your Windows Form application folder (where the application EXE will reside).
基本上,使用 Visual Studio,您可以将所有解决方案的项目设置为构建到同一个输出文件夹中,并将此文件夹用作 Windows 窗体应用程序文件夹(应用程序 EXE 将驻留在该文件夹中)。
By doing this, you will coordinate all of the possible assemblies references that your app is depend on.
通过这样做,您将协调您的应用程序所依赖的所有可能的程序集引用。
In VS 2012, right-click on a Project => Properties => Select Build (left pane) => Set your Output path:
在 VS 2012 中,右键单击项目 => 属性 => 选择构建(左窗格) => 设置输出路径:


I would select a a solution-level folder as the Output path.
我会选择一个解决方案级别的文件夹作为输出路径。
And if it's prohibited to perform such a modification at your workplace so I would suggest you to use dependency analysis tools like the following in order to interrogate and gather the appropriate assemblies that your app is depend on and will require at run-time:
如果禁止在您的工作场所执行此类修改,那么我建议您使用如下所示的依赖项分析工具,以便查询和收集您的应用程序依赖并在运行时需要的适当程序集:
Update:
更新:
Using the above mentioned tools will not yields assemblies references which are late-bounded (at run-time), for this case you may use: Fusion (the Assembly Binding Log Viewer)
使用上述工具不会产生后期绑定的程序集引用(在运行时),对于这种情况,您可以使用:Fusion(程序集绑定日志查看器)
回答by Matt Penner
Check out the Fody/Costura recommendation from this question: Embedding DLLs in a compiled executable
从这个问题中查看 Fody/Costura 的建议: 在编译的可执行文件中嵌入 DLL
It's great! I just tried it out for a similar need and in less then a few minutes I had a completely portable (except the .Net framework) exe that I could easily give to co-workers.
这很棒!我只是出于类似的需要尝试了它,然后在不到几分钟的时间里,我就拥有了一个完全可移植的(.Net 框架除外)exe,我可以轻松地将其提供给同事。

![C# 不使用媒体播放器播放视频 [Winform]](/res/img/loading.gif)