使用PowerShell批量修改项目文件的版本号

      在有些项目中,解决方案中包含有很多项目文件(类库、应用程序等),一个个修改AssemblyInfo.cs文件的版本信息很麻烦。

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
 

        将下面的代码保存为ps1文件,放到和解决方案同一目录下:

#获取解决方案的目录
$SolutionPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
#获取所有的AssemblyInfo.cs文件
$FileList = Get-ChildItem $SolutionPath -Recurse AssemblyInfo.cs
$oldVersion = "Version\(""\d{1,}.\d{1,}.\d{1,}.\d{1,}""\)"
$newVersion = "Version(""1.1.1.1"")"
Foreach($filename in $FileList)
{
    $filename = $filename.FullName 
    (Get-Content $filename) |  
    Foreach-Object { $_ -replace $oldVersion, $newVersion } |  
    Set-Content $filename 
}
 
posted @ 2012-06-01 01:42  Asharp  阅读(1741)  评论(0编辑  收藏  举报