1

I'm looking to define one location in my project to store a version string. The behavior that I'm looking for is that I store the version in a text file (version.txt) in my project directory, and then my pre-build event updates the content in Properties/AssemblyInfo.cs.

[assembly: AssemblyVersion("<Version>")] [assembly: AssemblyFileVersion("<Version>")] 

Yes, I know that there are only two fields and that they are right next to each other, but I'd still like to know if this is possible.

I wrote and tested this Powershell script that carries out this functionality.

$assemblyInfoPath = "Properties\AssemblyInfo.cs" $versionText = (Get-Content -Path version.txt -ReadCount 0) $assemblyInfoText = (Get-Content -Path $assemblyInfoPath -ReadCount 0) $assemblyInfoText = $assemblyInfoText -replace '\[assembly: AssemblyVersion\("((\d)+|(\.))*"\)\]', "[assembly: AssemblyVersion(`"$versionText`")]" $assemblyInfoText -replace '\[assembly: AssemblyFileVersion\("((\d)+|(\.))*"\)\]', "[assembly: AssemblyFileVersion(`"$versionText`")]" | Set-Content -Path $assemblyInfoPath 

What's failing (I think) is the invocation during the pre-build step:

Pre-build event command line:

powershell $(ProjectDir)set_assembly_version.ps1 

I haven't tried running this with msbuild, only through Visual Studio 2017. Is this even possible since AssemblyInfo.cs is a project file?

1
  • Changing source code as part of build is not the best idea... but what you are trying to do should work. Set build logging level (options->build and run) to detailed (or diagnostic) and read through log carefully... Commented Aug 27, 2019 at 2:01

2 Answers 2

2

I was trying to do the same thing, but found that my system wasn't configured to allow me to execute powershell scripts via command like that. I could have fixed that, but rather than require everyone do the same edits to their machines, I figured I would build a full pre-build command that wouldn't require an external script file be executed. The main 'challenge' was making sure I had escaped everything properly.

My version had quite a few changes due to the nature of my project. I tried to recreate exactly what you were trying to do in a command you should be able to plugin to your pre-build event:

powershell $assemblyInfoPath = \"$(projectDir)Properties\AssemblyInfo.cs\";$versionText = (Get-Content -Path \"$(projectDir)version.txt\" -ReadCount 0);$assemblyInfoText = (Get-Content -Path $assemblyInfoPath -ReadCount 0);$assemblyInfoText = $assemblyInfoText -replace \"^\[assembly: AssemblyVersion\(".*"\)\]$\", \"[assembly: AssemblyVersion(`\"$versionText`\")]\";$assemblyInfoText = $assemblyInfoText -replace \"^\[assembly: AssemblyFileVersion\(".*"\)\]$\", \"[assembly: AssemblyFileVersion(`\"$versionText`\")]\";Set-Content -Path $assemblyInfoPath -Value $assemblyInfoText; 
Sign up to request clarification or add additional context in comments.

Comments

0

What's failing (I think) is the invocation during the pre-build step:

Any error message? When we set command in pre-build event, it actually call the cmd.exe to execute the command. So you should make sure this script executes successfully in cmd.exe, then you can use it in pre-build event.

And I notice that you use relative path Properties\AssemblyInfo.cs in your ps script, this can be recognized by msbuild since when build this project, msbuild know the current ProjectDir, so it knows where to find the Properties\AssemblyInfo.cs. But it may cause error when this script is executed by cmd.exe if it doesn't know the current project directory. Please check this point.

I haven't tried running this with msbuild, only through Visual Studio 2017. Is this even possible since AssemblyInfo.cs is a project file?

Note: MSbuild is the build engine of VS. So even when you build the C# project in VS, you're actually calling the msbuild.exe to build them.(For VS2017)

Msbuild will compile the source files(including Assembly.cs file) in CoreCompile Target or (other compile targets?). Change the Build output log verbosity to Normal then you can see the build order. See this:

enter image description here

PreBuildEvent will execute before the Compile target, so it's possible. However, during build process, sometimes VS will occupy source files, so I'm not sure if your approach can finally succeed. If there's any error message, feel free to share it:)

And what's reason you need to set the version in pre-build event, maybe you can use this approach to automatically increase the version. For VS2017, change the Deterministic property in xx.csproj from true to false and it works to increase the assembly version automatically during build.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.