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...
A fairly old question with an accepted answer, but I also (due to limitations on the CI build server) wanted to do this via a batch file instead of powershell or MS Build Tasks. Here is what I came up with - assumes the solution directory is the current directory:
SetVersion.bat
Explanation
Basically this will recurse all the subfolders and identify AssemblyInfo.cs files, and then line-by-line copy the files to a new file. If any of the assembly version entries are found they are omitted. Then, at the end of each file, it appends the supplied version numbers.
For those batch gurus out there, some might ask why I prefer using
for /F
withdir /s
instead offor /R
to iterate over the files. Basically, its because I find the recursive for to be a bit clunky since it will execute for all sub-directories (not just matching files) when no wildcards are supplied. Using thedir
command is more consistent and easier to predict.Note: This batch file will remove all empty lines from the AssemblyInfo.cs files
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:
Here's a form that uses regular expressions to replace whatever version number is there:
Isn't there an MS Build task for modifying the assemblyInfo.cs
we have something like this in our publish.proj
This is the MSBuild target code where I update all my assemblyinfo.cs files (You have to init AssemblyInfoFilesToUpdate items collections before using this target):
I'm using FileUpdate task from MSBuildCommunityTasks.
Use the
WriteCodeFragment
MSBuild Task. Refer to this answer for an example.