Search code examples
visual-studioprojects-and-solutionsbuild-events

Visual studio to override file on selected build configuration


at the moment my project has a license file. No extension attached to the file. The file itself only contains a key.

I have 3 build configuration

Dev
Stage
Prod

At the moment I have 4 license file.

GLicense 

GLicense_dev
GLicense_stage
GLicense_prod

I tried to use #c preprocessor directive but the third party dll requires me to have the license name exactly as GLicense. The next approach I was thinking of taking was overriding the content of GLicense when building. I was wondering how abouts do I do that?


Solution

  • Modify your Projects pre-build command line to include the desired source file for each configuration

    copy /y "$(ProjectDir)GLicense_dev" "$(ProjectDir)$(OutDir)GLicense"
    
    copy /y "$(ProjectDir)GLicense_stage" "$(ProjectDir)$(OutDir)GLicense"
    
    copy /y "$(ProjectDir)GLicense_prod" "$(ProjectDir)$(OutDir)GLicense"
    

    Your Project file will look something like this

    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'dev|AnyCPU' ">
      <PreBuildEvent>copy /y "$(ProjectDir)GLicense_dev.txt" "$(ProjectDir)$(OutDir)GLicense.txt"</PreBuildEvent>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'stage|AnyCPU' ">
      <PreBuildEvent>copy /y "$(ProjectDir)GLicense_stage.txt" "$(ProjectDir)$(OutDir)GLicense.txt"</PreBuildEvent>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'prod|AnyCPU' ">
      <PreBuildEvent>copy /y "$(ProjectDir)GLicense_prod.txt" "$(ProjectDir)$(OutDir)GLicense.txt"</PreBuildEvent>
    </PropertyGroup>