0

How to make the following connection, so that a value assigned at the build execution is used in a *.cs file:

# command line build execution
msbuild -p:THE_VARIABLE="NEW_VALUE" TheProject.sln

# App.xaml.cs
namespace TheProject
{
    sealed partial class App : Application
    {
        private const string TheVariable = THE_VARIABLE;
    }
}

UPDATE 1:

Is there a way to access the csproj as an object from the App.xaml.cs ?

# command line build execution
msbuild -p:MSBUILD_CUSTOM_P="NEW_VALUE" TheProject.sln

# TheProject.csproj
<Target Name="CreateMSBUILD_CUSTOM_P" BeforeTargets="BeforeBuild" Condition=" $(MSBUILD_CUSTOM_P) != '' ">
  <PropertyGroup>
    <THE_VARIABLE>$(MSBUILD_CUSTOM_P)</THE_VARIABLE>
  </PropertyGroup>
</Target>

# App.xaml.cs
namespace TheProject
{
    sealed partial class App : Application
    {
        private const string TheVariable = CSPROJ.THE_VARIABLE;
    }
}
 
  • 1
    No. But you can write custom MSBuild targets in c# code to perform processing on your files, say with a Regex to rewrite them. But... I wouldn't reccomend it. Perhaps explain what you're *really* trying to do – pinkfloydx33 Mar 01 '21 at 09:58
  • It is an automation. The rewritten value in the line in that file would live only during a CD run, so what you suggested sounds like a solution for my case. How would the regex replace look like ? – Aleksandar Ivanov Mar 01 '21 at 10:03
  • 1
    If it's some sort of per-environment/deployment settings, store it in and read it from configuration files. Don't do what you're trying to do – pinkfloydx33 Mar 01 '21 at 10:05
  • 1
    Does this answer your question? [how to replace string in file using msbuild?](https://stackoverflow.com/questions/7837644/how-to-replace-string-in-file-using-msbuild) – pinkfloydx33 Mar 01 '21 at 10:06
  • 1
    Why not set an environment variable? – Brandon Boone Mar 03 '21 at 02:31

0 Answers0