-1

I have a solution with 25 C# projects aprox.

For every compilation of the solution, I tweak some project configuration usings precompilers directives.

When I need to add a directive to a configuration. I have to go to every project and add that directive. When I add a new configuration it is the same an worse.

In a C++ project I would had a .h file that would be included in all projects, to have something like a global configuration file.

But, how can I do something like that using Visual Studio and C#?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Fernando
  • 2,131
  • 3
  • 27
  • 46
  • Not exactly your answer but possibly an hint in the right direction http://stackoverflow.com/questions/10086591/create-multiple-versions-of-a-project-in-visual-studio-using-build-configuration/10086766#10086766 – Steve Jan 11 '16 at 18:02
  • 1
    https://msdn.microsoft.com/en-us/library/92x05xfs.aspx – Hans Passant Jan 11 '16 at 18:03
  • @Steve thanks for your suggestion, that is what I am doing right now. But it does not scale well when you have tenths of posible configurations and more than 20 related projects. (I know there is something inherently wrong with the code, but I can not change that) – Fernando Jan 11 '16 at 20:33

2 Answers2

1

A good way to do this would be to reference external config files from each projects configuration.

You can create just one config file with the information/directories that are frequently changing and reference it from each projects config files.

For example:

directives.confg:

<appSettings>
    <add key="ThisDirectory" value="This\Directory\Path"/>
    <add key="ThatDirectory" value="That\Directory\Path"/>
</appSettings>

And in your web/app config you would have:

<configuration>
    <appSettings file="C:\PathToYourExternalConfigFile\directives.config">
        <add key="OtherKeyNotInExternalConfig" value="SomeValue" />
    </appSettings>
</configuration>

Now only the directives.config file will need to be updated and all other projects will automatically have the latest config changes.

Matt Hensley
  • 908
  • 8
  • 18
  • It is for a WinForm application (not .NET) . Doing so will result in a runtime configuration, and I need a compile time. – Fernando Jan 11 '16 at 20:25
0

If you build your project using msbuild then all you need to do is set an environment variable with the same name as your define.

#if OPTION_ONE
    // option one code here 
#else
   // option one not set
#endif

Then from the developer command prompt you can build like so

set OPTION_ONE=true
msbuild YourSolution.sln 

If that doesn't work you could add the following to your project file

<DefineConstants Condition="'%(OPTION_ONE)' != ''">OPTION_ONE</DefineConstants>

Edit:

Create a common c# file for definitions the just like you would in c++ with a header. Create a user file (i.e. MyProject.csproj.user) to include the common cs file. The user file doesn't need to be included in the project. Visual studio will automatically use it if it exists:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
   <Compile Include="$(SolutionDir)CommonFile.cs"/>
</Project>

Copy that file to each project directory, changing the file name to match the project.

Or you can just add the common file as a link by adding existing item and click the option arrow next to Add and select Add As Link

jbriggs
  • 353
  • 2
  • 10
  • Thanks for your suggestion, but I need to change this value from Visual Studio because of debugging. Also the problem is not to set one define, but one define that, in times, sets severals others. – Fernando Jan 11 '16 at 20:24