Search code examples
c#visual-studiomsbuildprojectpreprocessor

Is there a way to know the active solution platform in Visual Studio?


I'm creating a C# solution in visual studio where I want slightly different behaviour in some of the projects depending on what the active solution platform is set to. See below in the configuration manager:

Configuration manager

Basically in some .csproj files I want to reference different resource projects depending on the active solution platform, kind of like this:

<ItemGroup Condition="'$(Platform)'=='x86'">
    <ProjectReference Include="..\Resources.x86\Resources.x86.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(Platform)'=='x64'">
    <ProjectReference Include="..\Resources.x64\Resources.x64.csproj" />
</ItemGroup>

The problem with the above is that my code itself is not platform specific, so all the projects will be compiled as AnyCPU, so the $(Platform) value will not reflect the active solution platform.

Is there another $ variable that I can use to get the active solution platform rather than the platform selected for the project? Or maybe there's a way I can attach a custom variable to the configuration manager which will reflect the value I want?

This would be easy if I was running the build via command line, then I could just pass in something like /p:ResourceProjectPath=..\Resources.x64\Resources.x64.csproj, but it seems more awkward to do from within VS.


Solution

  • No, there is no such variable.

    Check this official document:

    List of common macros

    From this you will know common variables unable to get this value.

    And other macros will be able to check in C++ project properties(Only CPP project has this interface to check all of the variables.):

    enter image description here

    In this place, you will know there is no built-in variable to achieve this.

    Then if you check the solution contents, you will find that there is no place where this information is stored in clear text(.sln only have the all of platforms, no active platform displayed.).

    I found a way that may be able to obtain this undisclosed information:

    dte.Solution.SolutionBuild.ActiveConfiguration

    Anyway this is not something that can be achieved with a simple csproj structure.