且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何不将 app.config 文件复制到输出目录

更新时间:2022-03-01 01:09:50

app.config 的处理比较特殊,它被处理为By Name,构建过程会选择下面的app.config文件这个顺序:

The handling of app.config is special, it is treated By Name, the build process will select the app.config file following this order:

  • 选择在主项目中设置的值 $(AppConfig).
  • 在与项目相同的文件夹中选择@(None) App.Config.
  • 在与项目相同的文件夹中选择@(Content) App.Config.
  • 在项目的任何子文件夹中选择 @(None) App.Config.
  • 在项目的任何子文件夹中选择 @(Content) App.Config.

$(AppConfig) 是一个 MSBuild 属性,如果它为空,它将在无"或内容"MSBuild 项组中查找名为App.Config"的文件,如果匹配,则该文件将被使用并将被复制到输出目录,用 [AssemblyName].config 替换 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

如果您想保留文件而不删除它,您需要将构建操作"属性更改为与无"或内容"不同的内容,您可以使用列表中的任何现有值(我建议"AdditionalFiles") 或您想使用的任何值,即MyConfigFile",现在将文件保存在项目中,但没有在输出目录中生成配置文件的逻辑.

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.

或者您可以将文件重命名为不同于app.config"的名称,并保留构建操作"和复制到输出目录"的当前属性值.

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".