wpf 如何不将 app.config 文件复制到输出目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40289509/
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 not copy app.config file to output directory
提问by David
I have a WPF application I am building. I am using Visual Studio Community 2015. In an effort to create a "true" release build, I am changing up some build settings so it only generates necessary files that will be installed on the user's computer. So no vshost exe, no generated XML files, etc. etc. etc. (I am retaining PDB file generation, however).
我有一个正在构建的 WPF 应用程序。我正在使用 Visual Studio Community 2015。为了创建一个“真正的”发布版本,我正在更改一些构建设置,以便它只生成将安装在用户计算机上的必要文件。所以没有 vshost exe,没有生成的 XML 文件,等等等等(但是,我保留了 PDB 文件生成)。
I have everything exactly how I want it, except for one file. I have an App.config file in each project. I don't mind this getting copied to the output directory on my debug builds, but on my "true" release build I don't want it there.
我拥有我想要的一切,除了一个文件。我在每个项目中都有一个 App.config 文件。我不介意这在我的调试版本中被复制到输出目录,但是在我的“真实”版本版本中我不希望它在那里。
The file is already set as "Do not copy" in its properties. See images below:
该文件已在其属性中设置为“不复制”。见下图:
Despite having the "Do not copy" setting, however, I still get an "MyAppName.exe.config" file generated every single time I build the project. Why is this so? How can I turn it off? It is quite annoying.
但是,尽管具有“不复制”设置,但每次构建项目时我仍然会生成一个“MyAppName.exe.config”文件。为什么会这样?我怎样才能关闭它?这很烦人。
Thanks for any advice!
感谢您的任何建议!
回答by Rolo
The handling of app.config is special, it is treated By Name, the build process will select the app.config file following this order:
app.config 的处理比较特殊,它是按 Name处理的,构建过程会按照这个顺序选择 app.config 文件:
- Choose the value $(AppConfig) set in the main project.
- Choose @(None) App.Config in the same folder as the project.
- Choose @(Content) App.Config in the same folder as the project.
- Choose @(None) App.Config in any subfolder in the project.
- Choose @(Content) App.Config in any subfolder in the project.
- 选择在主项目中设置的值 $(AppConfig)。
- 在与项目相同的文件夹中选择@(None) App.Config。
- 在与项目相同的文件夹中选择@(Content) App.Config。
- 在项目的任何子文件夹中选择 @(None) App.Config。
- 在项目的任何子文件夹中选择 @(Content) App.Config。
$(AppConfig) is an MSBuild Property, if it is empty, it will look for a file with name "App.Config" in the "None" or "Content" MSBuild Item Groups, if there's a match, the file will be used and will be copied to the output directory replacing the app.config name by [AssemblyName].config
$(AppConfig) 是一个 MSBuild 属性,如果它为空,它将在“无”或“内容”MSBuild 项组中查找名为“App.Config”的文件,如果匹配,将使用该文件并将被复制到输出目录,用 [AssemblyName].config 替换 app.config 名称
If you want to keep the file without delete it, you will need to change the "Build Action" property to something different to "None" or "Content", you can use any existing value on the list (I suggest "AdditionalFiles") or any value you want to use i.e. "MyConfigFile", and now the that will keep the file inside the project, but without the logic that generates the configuration file in the output directory.
如果您想保留文件而不删除它,您需要将“构建操作”属性更改为与“无”或“内容”不同的内容,您可以使用列表中的任何现有值(我建议使用“AdditionalFiles”)或您想使用的任何值,即“MyConfigFile”,现在将把文件保存在项目中,但没有在输出目录中生成配置文件的逻辑。
Or you can rename the file to something different than "app.config" and keep the current property values for "Build Action" and "Copy to Output Directory".
或者,您可以将文件重命名为不同于“app.config”的名称,并保留“构建操作”和“复制到输出目录”的当前属性值。


