.net – MSbuild for updating the assemblyinfo file

automationbuild-automationmsbuildnet

I am writing a batch file to automate a series of tasks. One of these tasks is to update the version of the dlls in my solution, by editing the assemblyinfo.cs file in the various projects in my solution; and then finally calling msbuild.exe to compile the solution.

In this regard, is it possible to write a command line script to update the various assemblyinfo.cs files in my .net solution. I would prefer to call msbuild from the commandline itself and not create another msbuild scripted file.

How does one do this using MSBuild? Is there any other way to do it?

Thanks for your time…

Best Answer

Editing files with dos cmd batch files is pretty hairy without the help of other tools. You would need to use something like the for /f command to step through the lines, and then process each line. For example look for the line that starts: "[assembly: AssemblyVersion" and replace it with something else.

However, if you don't have much in your AssemblyInfo.cs (and remember you can split the AssemblyInfo.cs into multiple cs files if you want) I'd suggest creating the file from scratch with a couple of echo statements.

If you have other tools available like sed.exe the edits can be done easily.

My preference these days would be to go for a trivial powershell script, that could eat this for breakfast and gives you access to the .Net libraries if you need it.

Here's a templating form of it:

(Get-Content AssemblyInfo.template.cs) -replace "{version}","1.2.3.4" > AssemblyInfo.cs

Here's a form that uses regular expressions to replace whatever version number is there:

$x = 'Version("{0}")' -f "1.2.3.4"
$content = Get-Content AssemblyInfo.cs
$content -replace 'Version\(".*"\)',$x > AssemblyInfo.cs