Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
I wrote this script to update all the AssemblyInfo.cs files in a Visual Studio solution, to have the same version number. This is the kind of thing I would have used Perl to do, in the past. But Powershell's got this covered nicely.
[ 23 April 2008 - 1150am PST - I updated the script to deal with the AssemblyInfo.vb files in a VB project, and also to deal with version numbers that have less than 4 digits. Thanks for the suggestion, Jason! ]
Maybe someone can learn from this.
# SetVersion.ps1
#
# Set the version in all the AssemblyInfo.cs or AssemblyInfo.vb files in any subdirectory.
#
# usage:
# from cmd.exe:
# powershell.exe SetVersion.ps1 2.8.3.0
#
# from powershell.exe prompt:
# .\SetVersion.ps1 2.8.3.0
#
# last saved Time-stamp: <Wednesday, April 23, 2008 11:46:40 (by dinoch)>
#
function Usage
{
echo "Usage: ";
echo " from cmd.exe: ";
echo " powershell.exe SetVersion.ps1 2.8.3.0";
echo " ";
echo " from powershell.exe prompt: ";
echo " .\SetVersion.ps1 2.8.3.0";
echo " ";
}
function Update-SourceVersion
{
Param ([string]$Version)
$NewVersion = 'AssemblyVersion("' + $Version + '")';
$NewFileVersion = 'AssemblyFileVersion("' + $Version + '")';
foreach ($o in $input)
{
Write-output $o.FullName
$TmpFile = $o.FullName + ".tmp"
get-content $o.FullName |
%{$_ -replace 'AssemblyVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)', $NewVersion } |
%{$_ -replace 'AssemblyFileVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)', $NewFileVersion } > $TmpFile
move-item $TmpFile $o.FullName -force
}
}
function Update-AllAssemblyInfoFiles ( $version )
{
foreach ($file in "AssemblyInfo.cs", "AssemblyInfo.vb" )
{
get-childitem -recurse |? {$_.Name -eq $file} | Update-SourceVersion $version ;
}
}
# validate arguments
$r= [System.Text.RegularExpressions.Regex]::Match($args[0], "^[0-9]+(\.[0-9]+){1,3}$");
if ($r.Success)
{
Update-AllAssemblyInfoFiles $args[0];
}
else
{
echo " ";
echo "Bad Input!"
echo " ";
Usage ;
}
Anonymous
April 21, 2008
PingBack from http://microsoftnews.askpcdoc.com/?p=3491
Anonymous
April 22, 2008
Nice work.
Considering your regex patterns don't specify <> or [] surrounding the attribute text you could easily extend this script to update both AssemblyInfo.vb and AssemblyInfo.cs just by making the lines referencing ".cs" a bit smarter.
Regards,
Anonymous
April 23, 2008
Good suggestion, Jason! I updated the script ...
Anonymous
November 30, 2008
The comment has been removed
Anonymous
November 15, 2012
Cheeso Hello, I want to leave my thanks, you really helped me with this script.
But maybe I can also leave a small contribution, I had a problem with the version above, when used together with a version control system like Mercurial, the file encoding is not preserved and Mercurial now think it is a binary file.
Adding the following lines before "move-item $TmpFile $o.FullName -force" command solves the problem.
# Preserve UTF-8 encoding of AssemblyInfo tmp file
$utf8Content = Get-Content $TmpFile -encoding utf8
[System.IO.File]::WriteAllLines($TmpFile, $utf8Content)
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in