0

Is there a way in Visual Studio to define some files or resources to be used only in specific build configurations? We use many #if DEBUG clauses to add debug-only code, but the same can't be done in a .config xml file, for example. Is there a way to define two versions of that file to be used, depending on whether the build configuration is set to Debug or Release?

I found this that describes what I'm looking to do but for Android using Ant. Is there a similar mechanism for Visual Studio and .NET?

DMX David Cardinal
  • 599
  • 2
  • 7
  • 19

1 Answers1

2

Is there a way in Visual Studio to define some files or resources to be used only in specific build configurations?

According to this doc: MSBuild concepts which mentioned “MSBuild provides a basic XML schema that you can use to control how the build platform builds software.”, I think MSBuild could help you solve this issue.

Is there a way to define two versions of that file to be used, depending on whether the build configuration is set to Debug or Release?

MSBuild has four parts: properties, items, tasks, and targets. For this issue you may need to focus on “items” part.

Solution:

You could add this command line “Condition=”’$(Configuration)’==’Debug’” and “Condition=”’$(Configuration)’==’Release’” to control when(Debug/Release) to use different versions of resources.

Refer to following screenshot, and this link(List of common properties and parameters-- Condition).

enter image description here

大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
  • Amazing! Exactly what I was looking for! However, I see how I can include some files in Debug and other files in Release, but if both versions of the file are called "MyFile.config", how can I use the solution you described to include a version or the other? Thanks! – DMX David Cardinal Jun 07 '20 at 23:59
  • @DCD You can try to use `` to specified the path of your different .config files. Besides, you can also consider renaming the .config file such as “MyFile.Debug.config” and “MyFile.Release.config”. BTW, you can refer to this doc: [Choose element(MSBuild)](https://learn.microsoft.com/en-us/visualstudio/msbuild/choose-element-msbuild?view=vs-2019) and maybe it’s another approach. – 大陸北方網友 Jun 08 '20 at 03:38
  • works perfectly! Thanks! Do you think you could add that little bit of information to your answer? I think it would make it more complete. Thanks again! – DMX David Cardinal Jun 08 '20 at 04:50