1

As part of a build process, I need to replace some of the code in a c# project at pre-build time. I hoped there is some hook available to specify in .csproj file. I am using Rider. This article appears to be about what I need; https://learn.microsoft.com/en-us/visualstudio/ide/reference/pre-build-event-post-build-event-command-line-dialog-box?view=vs-2019, but I am not sure how to use it in .csproj.

How do you specify pre-build actions in c#?

Trevor
  • 7,777
  • 6
  • 31
  • 50
Robert Segdewick
  • 543
  • 5
  • 17
  • Does this answer your question? [Visual Studio add pre-build event that always runs (C# project)](https://stackoverflow.com/questions/28916414/visual-studio-add-pre-build-event-that-always-runs-c-project) – gunr2171 Nov 23 '20 at 14:27

2 Answers2

1

You could define a BeforeBuild target in your csproj to run a command. Say, for example, I had a PS script I wanted to run:

<Target Name="BeforeBuild">                                                                    
   <Exec Command="$(PowerShellExe) -NonInteractive -executionpolicy Unrestricted 
             -command &quot;&amp; invoke-command -scriptblock { 
                      &amp;&apos;$(ScriptLocation)&apos; 
                      &apos;$(LogFileLocation)&apos;  
                      &apos;$(MSDeployComputerName)&apos;}
                      &quot;"/>    
</Target>

See docs.

dsosa
  • 71
  • 5
0

Inside the .csproj you can add this:

<PropertyGroup>
    <PreBuildEvent>start %25comspec%25</PreBuildEvent>
</PropertyGroup>

This only starts a cmd Window in that case but you can replace it. Also you can place it in the property group you want to use. See docs

Also I want to note the preprocessor directives specifically #if. When you want to replace DEBUG Code, that only should be present in DEBUG Environment and it's a static replacement, this would be easier.

JP-Hundhausen
  • 70
  • 1
  • 7