代码之家  ›  专栏  ›  技术社区  ›  Ian G

使用Visual Studio时是否可以自动增加文件生成版本?

  •  328
  • Ian G  · 技术社区  · 16 年前

    我只是想知道我怎么能 使用Visual Studio(2005)增加我的文件的生成(和版本?)。

    如果我查一下say的属性 C:\Windows\notepad.exe ,版本选项卡显示“文件版本:5.1.2600.2180”。我也想在我的dll版本中得到这些很酷的数字,而不是版本1.0.0.0,让我们面对它有点枯燥。

    我尝试了一些东西,但它似乎不是现成的功能,或者我只是找错了地方(像往常一样)。

    我主要从事网络项目。。。。

    我看了两个:

    1. http://www.codeproject.com/KB/dotnet/Auto_Increment_Version.aspx
    2. http://www.codeproject.com/KB/dotnet/build_versioning.aspx

    我不敢相信这么多的努力是标准的做法。

    编辑: http://www.codeproject.com/KB/dotnet/AutoIncrementVersion.aspx )

    23 回复  |  直到 12 年前
        1
  •  447
  •   Sam Meldrum    16 年前

    在VisualStudio2008中,以下内容起作用。

    [assembly: AssemblyVersion("1.0.0.0")]
    [assembly: AssemblyFileVersion("1.0.0.0")]
    

    您可以尝试将此更改为:

    [assembly: AssemblyVersion("1.0.*")]
    [assembly: AssemblyFileVersion("1.0.*")]
    

    但这并不会给你想要的结果,你最终会得到一个产品版本的 和的文件版本 . 不是你想要的!

    但是,如果删除这些行中的第二行,并且仅具有:

    [assembly: AssemblyVersion("1.0.*")]
    

    然后,编译器将文件版本设置为与产品版本相同,您将获得自动递增的产品和文件版本的所需结果,这两个版本是同步的。例如。 1.0.3266.92689

        2
  •  156
  •   abatishchev Karl Johan    11 年前

    打开AssemblyInfo.cs文件并进行更改

    // 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")]
    

    [assembly: AssemblyVersion("1.0.*")]
    //[assembly: AssemblyFileVersion("1.0.0.0")]
    

    您可以在IDE中通过转到project->房地产->装配信息

    但是,这只允许您自动增加程序集版本,并将为您提供

    程序集文件版本:此字段中不允许使用通配符(*)

    如果尝试在文件版本字段中放置*,则显示消息框。

        3
  •  55
  •   Christian    12 年前

    版本 MSBuild.Community.Tasks . 只需下载他们的安装程序,安装它,然后修改下面的代码,然后粘贴它 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> 在你的 .csproj 文件:

    <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
    <Target Name="BeforeBuild">
        <Version VersionFile="Properties\version.txt" Major="1" Minor="0" BuildType="Automatic" StartDate="12/31/2009" 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\VersionInfo.cs"
                      AssemblyVersion="$(Major).$(Minor)"
                      AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)" />
    </Target>
    

    注: It currently does not use the invariant culture.

    对于2010年1月14日的第三次构建,这将创建一个 VersionInfo.cs

    [assembly: AssemblyVersion("1.0")]
    [assembly: AssemblyFileVersion("1.0.14.2")]
    

    然后必须将该文件添加到项目中(通过 添加现有项目 ),以及 AssemblyVersion AssemblyFileVersion 线路必须从中移除 AssemblyInfo.cs .

    $(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.chm 版本属性

        4
  •  29
  •   Tohid John    8 年前

    我提出了一个类似于Christians的解决方案,但不依赖于社区MSBuild任务,这不是我的选项,因为我不想为所有开发人员安装这些任务。

    我正在生成代码并编译到程序集,希望自动增加版本号。但是,我不能使用VS 6.0.*AssemblyVersion技巧,因为它每天自动增加版本号,并破坏与使用旧版本号的程序集的兼容性。相反,我希望有一个硬编码的AssemblyVersion,但有一个自动递增的AssemblyFileVersion。我通过在AssemblyInfo.cs中指定AssemblyVersion并在MSBuild中生成VersionInfo.cs来实现这一点,如下所示,

      <PropertyGroup>
        <Year>$([System.DateTime]::Now.ToString("yy"))</Year>
        <Month>$([System.DateTime]::Now.ToString("MM"))</Month>
        <Date>$([System.DateTime]::Now.ToString("dd"))</Date>
        <Time>$([System.DateTime]::Now.ToString("HHmm"))</Time>
        <AssemblyFileVersionAttribute>[assembly:System.Reflection.AssemblyFileVersion("$(Year).$(Month).$(Date).$(Time)")]</AssemblyFileVersionAttribute>
      </PropertyGroup>
      <Target Name="BeforeBuild">
        <WriteLinesToFile File="Properties\VersionInfo.cs" Lines="$(AssemblyFileVersionAttribute)" Overwrite="true">
        </WriteLinesToFile>
      </Target>
    

    这将为AssemblyFileVersion生成一个具有AssemblyFileVersion属性的VersionInfo.cs文件,其中版本遵循YY.MM.DD.TTTT模式,并带有生成日期。您必须将此文件包含在项目中并使用它进行生成。

        5
  •  17
  •   Ryan Gates    12 年前
        6
  •  14
  •   Rahul    4 年前

    Automatic Versions 支持Visual Studio(2012、2013、2015)、2017和;2019

    屏幕快照 enter image description here

    enter image description here

        7
  •  13
  •   NoWar    12 年前

    要获取版本号,请尝试

     System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
     System.Reflection.AssemblyName assemblyName = assembly.GetName();
     Version version = assemblyName.Version;
    

     [assembly: AssemblyVersion("1.0.*")]
     [assembly: AssemblyFileVersion("1.0.*")]
    

    另请注意,第三个数字是自2000年2月1日以来的天数,第四个数字是一天中总秒数的一半。所以如果你在午夜编译,它应该是零。

        8
  •  8
  •   Dirk Vollmar    16 年前

    在AssemblyInfo中或在其他文章中所述的项目属性下的版本号中设置*不适用于所有版本的Visual Studio/.NET。

    Afaik它在VS 2005中不起作用(但在VS 2003和VS 2008中起作用)。对于VS 2005,您可以使用以下内容: Auto Increment Visual Studio 2005 version build and revision number on compile time .

    但是请注意,对于强名称程序集,不建议自动更改版本号。原因是,每次重建引用的程序集时,必须更新对此类程序集的所有引用,因为强命名程序集引用始终是对特定程序集版本的引用。只有在接口发生更改时,Microsoft才会更改.NET Framework程序集的版本号。(注意:我还在MSDN中搜索我读到的链接。)

        9
  •  6
  •   Andreas Reiff    12 年前


    基于Boog的解决方案(对我来说不起作用,可能是因为VS2008?),您可以使用生成文件的预构建事件的组合,添加该文件(包括其版本属性),然后使用一种方法再次读取这些值。那就是。。

    预构建事件:

    echo [assembly:System.Reflection.AssemblyFileVersion("%date:~-4,4%.%date:~-7,2%%date:~-10,2%.%time:~0,2%%time:~3,2%.%time:~-5,2%")] > $(ProjectDir)Properties\VersionInfo.cs
    

    返回日期的代码(从年到秒):

    var version = assembly.GetName().Version;
    var fileVersionString = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion;
    Version fileVersion = new Version(fileVersionString);
    var buildDateTime = new DateTime(fileVersion.Major, fileVersion.Minor/100, fileVersion.Minor%100, fileVersion.Build/100, fileVersion.Build%100, fileVersion.Revision);
    

    不太舒服。。此外,我不知道它是否会创建大量的力重建(因为文件总是更改)。

    例如,如果只每隔几分钟/小时更新一次VersionInfo.cs文件(通过使用临时文件,然后在检测到足够大的更改时复制/覆盖真实的VersionInfo.cs),则可以使其更智能。我曾经做得很成功。

        10
  •  6
  •   Community ƒernando Valle    4 年前

    2019年在Visual Studio中

    [assembly: AssemblyVersion("1.0.*")]
    

    指定的版本字符串不符合所需格式

    在我设置之后,格式最终被接受 Deterministic False 在里面 project.csproj

    <Deterministic>false</Deterministic>
    

    出于某种原因 确定性 错误的

    解决方法:

    生成后事件批处理脚本

    autoincrement_version.ps1 作为参数传递 AssemblyInfo.cs

    if $(ConfigurationName) == Release (
    PowerShell -ExecutionPolicy RemoteSigned $(ProjectDir)autoincrement_version.ps1 '$(ProjectDir)My Project\AssemblyInfo.cs'
    )
    

    它使用正则表达式自动增加修订号

    param( [string]$file );
      $regex_revision = '(?<=Version\("(?:\d+\.)+)(\d+)(?="\))'
      $found = (Get-Content $file) | Select-String -Pattern $regex_revision
      $revision = $found.matches[0].value
      $new_revision = [int]$revision + 1
      (Get-Content $file) -replace $regex_revision, $new_revision | Set-Content $file -Encoding UTF8
    
        11
  •  5
  •   James Curran    16 年前

    将版本号设置为“1.0.*”,它会自动在最后两个数字中填入日期(从某个点算起的天数)和时间(从午夜算起的半秒)

        12
  •  5
  •   Community ƒernando Valle    8 年前
        13
  •  4
  •   hal    8 年前

    Cake

    Setup(() =>
    {
        // Executed BEFORE the first task.
        var datetimeNow = DateTime.Now;
        var daysPart = (datetimeNow - new DateTime(2000, 1, 1)).Days;
        var secondsPart = (long)datetimeNow.TimeOfDay.TotalSeconds/2;
        var assemblyInfo = new AssemblyInfoSettings
        {
            Version = "3.0.0.0",
            FileVersion = string.Format("3.0.{0}.{1}", daysPart, secondsPart)
        };
        CreateAssemblyInfo("MyProject/Properties/AssemblyInfo.cs", assemblyInfo);
    });
    

    在这里:

    • FileVersion-是程序集文件版本。

    请注意,您不仅可以修补版本,还可以修补 also all other necessary information .

        14
  •  3
  •   MusiGenesis    16 年前

    转到项目|属性,然后是部件信息,然后是部件版本,并在最后一个或倒数第二个框中添加*号(不能自动增加主要或次要零部件)。

        15
  •  3
  •   user3638471 user3638471    8 年前

    {major}.{year}.1{date}.1{time}

    link ).

    生成的版本号变为 1.2016.10709.11641

    • 可怜的人零填充(用愚蠢的引导) 1
    • 几乎是人类可读的 地方的 嵌入到版本号中的日期时间
    • 将主要版本留给真正重大的突破性更改。

    将新项目添加到项目中,选择“常规”->文本模板,将其命名为 CustomVersionNumber AssemblyVersion AssemblyFileVersion 在里面 Properties/AssemblyInfo.cs .

    然后,在保存此文件或生成项目时,将重新生成 .cs 文件作为子项位于已创建的 .tt 文件

    <#@ template language="C#" #>
    <#@ assembly name="System.Core" #>
    <#@ import namespace="System.Linq" #>
    
    //
    // This code was generated by a tool. Any changes made manually will be lost
    // the next time this code is regenerated.
    //
    
    using System.Reflection;
    
    <#
        var date = DateTime.Now;
        int major = 1;
        int minor = date.Year;
        int build = 10000 + int.Parse(date.ToString("MMdd"));
        int revision = 10000 + int.Parse(date.ToString("HHmm"));
    #>
    
    [assembly: AssemblyVersion("<#= $"{major}.{minor}.{build}.{revision}" #>")]
    [assembly: AssemblyFileVersion("<#= $"{major}.{minor}.{build}.{revision}" #>")]
    
        16
  •  2
  •   devstuff    16 年前

    使用MSBuild社区任务中的AssemblyInfo任务( http://msbuildtasks.tigris.org/

    它有许多选项,包括将版本号与日期和时间绑定的选项。

    推荐。

        17
  •  2
  •   Deanna superuser    12 年前

    到目前为止,我的申请,

    string ver = Application.ProductVersion;
    

    ver = 1.0.3251.27860

    值3251是自2000年1月1日起的天数。我使用它在应用程序的初始屏幕上显示版本创建日期。当与用户打交道时,我可以询问创建日期,这比一些长数字更容易沟通。

    (我是一个支持小公司的单人部门。这种方法可能不适合你。)

        18
  •  2
  •   Atiris aybe    11 年前

        private bool IncreaseFileVersionBuild()
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                try
                {
                    var fi = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.GetDirectories("Properties")[0].GetFiles("AssemblyInfo.cs")[0];
                    var ve = System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
                    string ol = ve.FileMajorPart.ToString() + "." + ve.FileMinorPart.ToString() + "." + ve.FileBuildPart.ToString() + "." + ve.FilePrivatePart.ToString();
                    string ne = ve.FileMajorPart.ToString() + "." + ve.FileMinorPart.ToString() + "." + (ve.FileBuildPart + 1).ToString() + "." + ve.FilePrivatePart.ToString();
                    System.IO.File.WriteAllText(fi.FullName, System.IO.File.ReadAllText(fi.FullName).Replace("[assembly: AssemblyFileVersion(\"" + ol + "\")]", "[assembly: AssemblyFileVersion(\"" + ne + "\")]"));
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            return false;
        }
    


    使用此代码,您可以更新AssemblyInfo.cs中文件信息的任何部分(但必须使用“标准”目录结构)。

        19
  •  2
  •   Maxcelcat    11 年前

    更改AssemblyInfo在VS2012中起作用。在VisualStudio中没有更多的支持似乎很奇怪,您可能会认为这是构建/发布过程的一个基本部分。

        20
  •  2
  •   Telson Alva    7 年前

    我已经创建了一个应用程序来自动增加文件版本。

    1. 下载 Application
    2. 将以下行添加到预生成事件命令行

      C:\temp\IncrementFileVersion.exe$(SolutionDir)\Properties\AssemblyInfo.cs

    为了保持简单,应用程序仅在出现错误时才会抛出消息,要确认它工作正常,您需要在“程序集信息”中检查文件版本

    注意:您必须在Visual studio中为“程序集信息”按钮重新加载解决方案以填充字段,但是您的输出文件将具有更新版本。

    如需建议和请求,请通过telson向我发送电子邮件_alva@yahoo.com

        21
  •  1
  •   Sergiy    10 年前

    AssemblyInfoUtil . 自由的开源。

        22
  •  1
  •   Community ƒernando Valle    7 年前

    我正在使用这种方法 https://stackoverflow.com/a/827209/3975786 将T4模板放置在“解决方案项”中,并在每个项目中与“添加为链接”一起使用。

        23
  •  1
  •   Khawaja Asim    7 年前

    自动的 This article 将解决你的许多问题。

        24
  •  0
  •   Brian Knoblauch    16 年前

        25
  •  0
  •   John Denniston    4 年前

    对于任何使用Turtoise Subversion的人,您可以将其中一个版本号与源代码的Subversion版本号绑定。我觉得这很有用(审计员也很喜欢这个!)。您可以通过在预构建中调用WCREV实用程序并从模板生成AssemblyInfo.cs来完成此操作。


    "C:\Program Files\TortoiseSVN\bin\SubWCRev.exe" "$(ProjectDir)." "$(ProjectDir)Properties\AssemblyInfo.wcrev"  "$(ProjectDir)Properties\AssemblyInfo.cs"
    

    模板文件将包括wcrev令牌替换字符串:$wcrev$
    例如

    [assembly: AssemblyFileVersion("1.0.0.$WCREV$")]
    

    注: