Friday, January 27, 2017

Exclude files from getting compiled in Visual Studio Project Depending on the Platform Configuration

I had to include and maintain some SAP Business One system forms captured from each SAP Business One version (9.0, 9.1, and 9.2) for an SAP Business One Add-on Visual Studio project. Of course, I've tried with placing them in separate folders, but during the compilation of the source code, I've got this build error.



So, I thought of an out-of-the-box solution that could help me to compile my add-on project with only the source code files that are related to the SAP version and bit version. This was the summary of the R&D work that I did.

Problem : How can I explicitly exclude/include source code files in Visual Studio, depend on the Solution Configuration or Solution Platform.


Directions :
I've created a sample visual studio solution and added two empty classes.


Then I've opened up the ConsoleApplication1 project file using a text editor.


Then Noticed the usage of the Condition element and moved my Class1.cs and Class2.cs in to two separate item groups as below.



Now, When I switch the value to 32bit from the Solution platform, only Class1 is getting attached to the compiler.




Similarly, for 64bit, I only get Class2.




Since both Class1 and Class2 coexist in the solution at any time, this workaround will definitely help to get rid of build automation-related work, even if it's hard to maintain.