如何在构建时将其他文件包含到 C# 中的输出目录中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16785369/
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 include other files to the output directory in C# upon build?
提问by chad
I have some library files needed for my application to work.
My application has a setup and deployment included.
我的应用程序需要一些库文件才能工作。
我的应用程序包含设置和部署。
I already know that in order for a library file to be added to the output directory of the application when installing, I just have to reference those libraries inside the .NET IDE before building... the only problem is that these libraries can't be referenced... So I need to be able to copy these libraries to the installation directory of my application... At the moment, I am copying these libraries manually...
我已经知道,为了在安装时将库文件添加到应用程序的输出目录中,我只需要在构建之前在 .NET IDE 中引用这些库......唯一的问题是这些库不能被引用......所以我需要能够将这些库复制到我的应用程序的安装目录......此刻,我正在手动复制这些库......
Addendum
附录
I also did try to add these library files as an Existing Itemto my project and marked each library files' Copy to Output Directoryto Copy if neweron their properties but still not getting the solution I want.
我还没有尝试将这些库文件添加为现有项目的每个库文件到我的项目,标志着复制到输出目录,以复制是否有更新它们的性质,但仍没有得到我想要的解决方案。
Update 1
更新 1
Thanks for you help guys it helped me solve my problem, I managed to make the solutions you posted work except for one... @Matthew Watson's post.. I even managed to find a solution too so I wanted to share it with you also.
感谢您的帮助,它帮助我解决了我的问题,我设法使您发布的解决方案起作用,除了一个...@Matthew Watson 的帖子.. 我什至也设法找到了解决方案,所以我也想与您分享.
Heres what I did:
这是我所做的:
1. I opened the setup and deployment project in my application.
2. Under the Application Folder Tree, on it's right side, I right clicked..
3. then clicked Add..
4. then clicked File
5. and then browsed for the files I wanted to add to the installation directory
6. and click open.
But out of curiosity...I am still trying to make what @Matthew Watson posted work...Hope you can help me with this one guys. Thanks in advance
但出于好奇……我仍在努力使@Matthew Watson 发布的内容发挥作用……希望您能帮助我解决这个问题。提前致谢
Update 2
更新 2
I forgot to update this post yesterday, I already manage to make Matthew Watson's solution worked yesterday. Thank you again for all your help guys.
我昨天忘记更新这篇文章了,昨天我已经设法让 Matthew Watson 的解决方案起作用了。再次感谢大家的帮助。
采纳答案by Matthew Watson
You can add files to your project and select their properties: "Build Action"as "Content"and "Copy to output directory"as "Copy Always"or Copy if Newer(the latter is preferable because otherwise the project rebuilds fully every time you build it).
您可以将文件添加到您的项目并选择它们的属性:"Build Action"as"Content"和"Copy to output directory"as"Copy Always"或Copy if Newer(后者更可取,否则每次构建项目时都会完全重建)。
Then those files will be copied to your output folder.
然后这些文件将被复制到您的输出文件夹。
This is better than using a post build step because Visual Studio will know that the files are part of the project. (That affects things like ClickOnce applications which need to know what files to add to the clickonce data.)
这比使用后期构建步骤要好,因为 Visual Studio 会知道这些文件是项目的一部分。(这会影响 ClickOnce 应用程序之类的东西,这些应用程序需要知道将哪些文件添加到 clickonce 数据中。)
You will also be more easily able to see which files are in the project because they will be listed with the source code files rather than hidden in a post-build step. And also Source Control can be used with them more easily.
您还可以更轻松地查看项目中的哪些文件,因为它们将与源代码文件一起列出,而不是隐藏在构建后步骤中。而且源代码管理可以更轻松地与它们一起使用。
Once you have added "Content" files to your project, you will be able to add them to a Visual Studio 2010 Setup and Deployment project as follows:
将“内容”文件添加到项目后,您将能够将它们添加到 Visual Studio 2010 安装和部署项目,如下所示:
Go into your Setup project and add to your "Application Folder"output the Project Output called "Content Files". If you right-click the Content Files after adding them you can select "outputs" and see what it's going to copy.
进入您的安装项目,并将"Application Folder"名为 的项目输出添加到您的输出中"Content Files"。如果在添加内容文件后右键单击它们,您可以选择“输出”并查看它将要复制的内容。
Note that Setup and Deployment projects are NOT supported in Visual Studio 2012.
请注意,Visual Studio 2012 不支持安装和部署项目。
回答by Tigran
You can use Visual Studio Post Build Event - Copy to Relative Directory Location. Which are basically scripts that are executed on build of specified project.
您可以使用Visual Studio Post Build Event - Copy to Relative Directory Location。基本上是在构建指定项目时执行的脚本。
So you can use it to copy binaries you need, beforeactually running your application.
因此,在实际运行应用程序之前,您可以使用它来复制您需要的二进制文件。

