我只是想知道如何使用Visual Studio(2005)自动增加文件的构建(和版本?)

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

我尝试了一些东西,但它似乎没有开箱即用的功能,或者可能我只是在错误的地方(像往常一样)。

我主要工作与网络项目....

我看了两个:

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

我不敢相信花这么大力气做一件事是标准做法。

编辑: 据我所知,它在VS2005中不工作(http://www.codeproject.com/KB/dotnet/AutoIncrementVersion.aspx)


当前回答

也许,对于这个任务,你可以使用这样的代码:

    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;
    }

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

其他回答

有一个可视化工作室扩展Automatic Versions,支持visual studio(2017,2019和2022)

屏幕截图

在每次构建中更改版本号的另一个选项是使用MSBuild.Community.Tasks的version任务。只需下载他们的安装程序,安装它,然后调整以下代码,并将其粘贴到<Import Project="$(MSBuildBinPath)\Microsoft.CSharp。在你的。csproj文件中的Targets " />:

<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>

注意:根据您的语言环境调整StartDate属性。它目前不使用不变区域性。

对于2010年1月14日的第三个版本,这将创建一个包含以下内容的VersionInfo.cs:

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

然后必须将该文件添加到项目中(通过添加现有项),并且必须从AssemblyInfo.cs中删除AssemblyVersion和AssemblyFileVersion行。

用于更改版本组件的不同算法在$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks中描述。chm和版本属性。

它在项目属性下的Publish中

(~ http://screencast.com/t/Vj7rhqJO)

我想出了一个类似于基督徒的解决方案,但不依赖于社区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>

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

Cake支持对AssemblyInfo文件进行补丁。有了蛋糕在手,你有无限的方法来实现自动版本增量。

像c#编译器一样增加版本的简单例子:

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);
});

在这里:

Version -是汇编版本。最佳实践是锁定主要版本号并保留零(如“1.0.0.0”)。 FileVersion -是程序集文件版本。

请注意,您不仅可以修补版本,还可以修补所有其他必要的信息。