Vsiual Studio自動生成版本號的一種方法
1、安裝nuget包MSBuildTasks
2、編輯專案的csproj檔案,找到被註釋掉的target的beforebuild,去掉註釋,新增如下程式碼。程式碼如下。
<Target Name="BeforeBuild"> <Version VersionFile="Propertiesversion.txt" Major="1" Minor="0" BuildType="Automatic" StartDate="09/01/2017" RevisionType="BuildIncrement"> <Output TaskParameter="Major" PropertyName="Major" /> <Output TaskParameter="Minor" PropertyName="Minor" /> <Output TaskParameter="Build" PropertyName="Build" /> <Output TaskParameter="Revision" PropertyName="Revision" /> </Version> <AssemblyInfo CodeLanguage="CS" OutputFile="Properties\FileVersionInfo.cs" AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)" /> </Target>
3、編譯專案一次就會在專案資料夾下生成Propertiesversion.txt
4、在AssemblyInfo.cs檔案中包含了AssemblyVersion和AssemblyFileVersion,這裡把AssemblyFileVersion單獨放到了FileVersionInfo.cs中,編譯時會自動生成FileVersionInfo.cs檔案,其內容為AssemblyFileVersion,把該檔案包含到專案中即可。這裡並沒有讓程式自動生成AssemblyVersion。
這種方式生成版本號會在vs編譯時更新版本號。