打开 AssemblyInfo.cs
将 AssemblyVersion 改为如下:
改完后发现报错了,不用管,关闭项目。
将项目的 .csproj 文件用记事本或者其他的软件打开
找到 Deterministic 这个节点
将其改为 false,然后保存
重新打开项目,这时候就不会报错了
测试代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace 版本自动增加
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("程序集版本号:{0}", GetAssemblyVersion());
Console.WriteLine("程序集文件版本号:{0}", AssemblyFileVersion());
Console.ReadKey();
}
/// <summary>
/// 读取程序集版本号
/// </summary>
/// <returns></returns>
private static string GetAssemblyVersion()
{
var assembly = Assembly.GetExecutingAssembly();
return assembly?.GetName().Version?.ToString();
}
/// <summary>
/// 读取程序集文件版本号
/// </summary>
/// <returns></returns>
private static string AssemblyFileVersion()
{
object[] attributes = Assembly.GetExecutingAssembly()
.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);
if (attributes.Length != 0)
return ((AssemblyFileVersionAttribute)attributes[0]).Version;
else
return string.Empty;
}
}
}
运行:
不重新生成的话,AssemblyVersion 这里版本号不会变
当前的方式只能动态的改变最后一个号码,另一种方式是改变所有的号码
在你的项目中,用记事本打开 Test1.csproj,假设 Test1 是你的项目名,加入下面代码:
<Target Name="GenerateVersion" BeforeTargets="BeforeBuild">
<PropertyGroup>
<!-- 动态生成版本号 -->
<MajorVersion>1</MajorVersion>
<MinorVersion>$([System.DateTime]::UtcNow.Year)</MinorVersion>
<BuildNumber>$([System.DateTime]::UtcNow.DayOfYear)</BuildNumber>
<Hour>$([System.DateTime]::UtcNow.Hour)</Hour>
<Minute>$([System.DateTime]::UtcNow.Minute)</Minute>
<!-- 将小时和分钟拼接成一个字符串,确保两位数字 -->
<RevisionNumber>$([System.String]::Format("{0:D2}{1:D2}", $(Hour), $(Minute)))</RevisionNumber>
<!-- 组装完整版本号 -->
<Version>$(MajorVersion).$(MinorVersion).$(BuildNumber).$(RevisionNumber)</Version>
<!-- 指定生成的输出文件路径 -->
<GeneratedAssemblyInfoFile>$(IntermediateOutputPath)GeneratedAssemblyInfo.cs</GeneratedAssemblyInfoFile>
</PropertyGroup>
<ItemGroup>
<AssemblyAttributes Include="System.Reflection.AssemblyVersionAttribute">
<_Parameter1>$(Version)</_Parameter1>
</AssemblyAttributes>
<AssemblyAttributes Include="System.Reflection.AssemblyFileVersionAttribute">
<_Parameter1>$(Version)</_Parameter1>
</AssemblyAttributes>
</ItemGroup>
<WriteCodeFragment Language="C#" AssemblyAttributes="@(AssemblyAttributes)" OutputFile="$(GeneratedAssemblyInfoFile)" />
<!-- 包含生成的文件到编译中 -->
<ItemGroup>
<Compile Include="$(GeneratedAssemblyInfoFile)" />
</ItemGroup>
</Target>
完整的 Test1.csproj 如下:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C73F01E1-CBB7-4D5A-A6F5-B2861CE3503D}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Test1</RootNamespace>
<AssemblyName>Test1</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>false</Deterministic>
</PropertyGroup>
<Target Name="GenerateVersion" BeforeTargets="BeforeBuild">
<PropertyGroup>
<!-- 动态生成版本号 -->
<MajorVersion>1</MajorVersion>
<MinorVersion>$([System.DateTime]::UtcNow.Year)</MinorVersion>
<BuildNumber>$([System.DateTime]::UtcNow.DayOfYear)</BuildNumber>
<Hour>$([System.DateTime]::UtcNow.Hour)</Hour>
<Minute>$([System.DateTime]::UtcNow.Minute)</Minute>
<!-- 将小时和分钟拼接成一个字符串,确保两位数字 -->
<RevisionNumber>$([System.String]::Format("{0:D2}{1:D2}", $(Hour), $(Minute)))</RevisionNumber>
<!-- 组装完整版本号 -->
<Version>$(MajorVersion).$(MinorVersion).$(BuildNumber).$(RevisionNumber)</Version>
<!-- 指定生成的输出文件路径 -->
<GeneratedAssemblyInfoFile>$(IntermediateOutputPath)GeneratedAssemblyInfo.cs</GeneratedAssemblyInfoFile>
</PropertyGroup>
<ItemGroup>
<AssemblyAttributes Include="System.Reflection.AssemblyVersionAttribute">
<_Parameter1>$(Version)</_Parameter1>
</AssemblyAttributes>
<AssemblyAttributes Include="System.Reflection.AssemblyFileVersionAttribute">
<_Parameter1>$(Version)</_Parameter1>
</AssemblyAttributes>
</ItemGroup>
<WriteCodeFragment Language="C#" AssemblyAttributes="@(AssemblyAttributes)" OutputFile="$(GeneratedAssemblyInfoFile)" />
<!-- 包含生成的文件到编译中 -->
<ItemGroup>
<Compile Include="$(GeneratedAssemblyInfoFile)" />
</ItemGroup>
</Target>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
将 AssemblyInfo.cs 中的两个版本号注释
运行后:
end