首先 引入 MSBuildTasks这个包。
打开项目的 【.csproj】文件
找到最后相对应的文件(一般加上去的都是排在最后)
注释掉<!--<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">-->
更改为
<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>-->
<PropertyGroup>
</PropertyGroup>
<Error Condition="!Exists(‘..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.targets‘)" Text="$([System.String]::Format(‘$(ErrorText)‘, ‘..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.targets‘))" />
</Target>
如果有觉得一处一处修改的麻烦,下边是我吧原来生成的代码给删除 直接粘贴的代码。
修改完【.csproj】文件以后 打开 vs,打开隐藏文件 ,打开 Properties 把隐藏文件给包含在项目里边就可以了。
然后读取版本信息
var s = "";
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);
if (attributes.Length == 0)
{
s = "";
}
else
{
s = ((AssemblyFileVersionAttribute)attributes[0]).Version;
}
下面是修改过后的 【.csproj】
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.targets" Condition="Exists(‘..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.targets‘)" />
<!--<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">-->
<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>-->
<PropertyGroup>
</PropertyGroup>
<Error Condition="!Exists(‘..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.targets‘)" Text="$([System.String]::Format(‘$(ErrorText)‘, ‘..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.targets‘))" />
</Target>