AssemblyInfo版本信息星号

时间:2012-04-19 14:00:26

标签: c# .net

在AssemblyInfo.cs中为C#项目说明可以用*指定版本信息

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build Numbers 
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

我把它更改为:

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

这是我从编译器得到的错误:

error CS0647: Error emitting 'System.Reflection.AssemblyVersionAttribute' attribute -- 'The version specified '1.0.*.*' is invalid'
warning CS1607: Assembly generation -- The version '1.0.*.*' specified for the 'file version' is not in the normal 'major.minor.build.revision' format

它是如何(它甚至?)它的工作原理?

5 个答案:

答案 0 :(得分:64)

“自动”内部版本号的语法(参见MSDN)可以是:

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

或:

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

* 表示之后一切都是自动的。您不能拥有自动内部版本号和固定版本号,则此语法不正确:

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

对于AssemblyFileVersionAttribute,您无法使用 * 特殊字符,因此您必须提供完整且有效的版本号。请注意,如果您未提供 AssemblyFileVersionAttribute,那么您将自动获得正确的FileVersionInfo(使用相同版本的AssemblyVersionAttribute)。只有在需要设置不同的版本时才需要指定该属性。

答案 1 :(得分:27)

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

请记住注释AssemblyFileVersion行,否则自动生成的程序集版本将始终为“1.0.0.0”。

答案 2 :(得分:4)

在我看来,使用[assembly: AssemblyVersion("x.y.z.*")]Patch不应自动编号。例如:

  

[assembly:AssemblyVersion(" 1.2.3。*")]

使用' *'在AssemblyVersion中很好,但请关注seemver.org,我们应该使用*作为版本结构revision的{​​{1}}部分。

  

给定版本号MAJOR.MINOR.PATCH,增加:

     

进行不兼容的API更改时的MAJOR版本

     

以向后兼容的方式添加功能时的MINOR版本,

     

当您进行向后兼容的错误修复时的PATCH版本。

答案 3 :(得分:2)

那为什么提供的评论说

// 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.*")]
[assembly: AssemblyFileVersion("1.0.0.0")]

但是生成生成CS8357吗? 没有人得到备忘录。

Work around:

 1. Close all open documents
 2. In the Solution Explorer, right-click the Project and select Unload Project
 3. In the Solution Explorer, right-click the Project (now marked as unavailable) and select Edit to access the `.CSPROJ` file
 4. In the opened window, find `<Deterministic>true</Deterministic>` and change it to `<Deterministic>false</Deterministic>`
 5. Save the file and ensure that the edit window is closed
 6. In the Solution Explorer, right-click the Project and select Reload Project

Your build (should then) work. :)

答案 4 :(得分:0)

我已经使用版本中的“*”来简化控制版本,并为客户提供信息。 我有一个在手持设备(PointOfSell)中运行的小程序,有时会发生更新。 :-) 在我的代码中,我使用:

来自 AssemblyInfo.cs 的片段

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// 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.*")]
// Need to comment this line, 
//[assembly: AssemblyFileVersion("1.0.0.0")]
//to avoid the following error:
// ...\Properties\AssemblyInfo.cs(36,32,36,39):
// warning CS7035: The specified version string does not conform to 
// the recommended format - major.minor.build.revision
// warning AL1053: The version '1.0.*' specified for 
// the 'file version' is not in the normal 'major.minor.build.revision' format

检查更新时,我从一个小型 Web 服务向客户端发送版本列表。

void smsReceive1_OnCabUpdate(object sender, CabDataEventArgs e)
{
    try
    {
        var cabVer = e.CabVersion;
        var sms1 = (SmsService.SmsService)sender;
        SmsService.CabinetVersion progVer = null;
        var exeApp = Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
        foreach (var item in cabVer)
        {
            if (string.Compare(item.FileName, exeApp, true) == 0)
            {
                progVer = item;
                break;
            }
        }

        var msgForm = new MessageBoxForm();
        if (!forceUpdate && !WillUpdateVersion(progVer))
        {
            Buzzer.Warning();
            sms1.StopReceive();
            msgForm.Message = "\r\nNewest Version!\r\n" +
                "Version\r\n" + progVer.AssemblyVersion.ToString() + "\r\n" +
                "Last Modified:\r\n" + progVer.AssemblyVersion.ToDateTime().ToString("yy/MM/dd HH:mm") + "\r\n" +
                "\r\nNo need to update";
            msgForm.OKShowDialog();
            return;
        }
        // 
        byte[] buffer = e.CabData;
        var filename = "\\ramdisk\\Setup.cab";
        if (File.Exists(filename))
        {
            File.Move(filename, string.Format("{0:yyMMdd_HHmmss}_Setup.cab"));
        }
        var fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
        fs.Write(buffer, 0, buffer.Length);
        fs.Flush();
        fs.Close();
        //
        Buzzer.Warning();
        //Stop SmsReceive
        sms1.StopReceive();
        msgForm.Message = "There is an update.\r\n" +
            "Version \r\n" + progVer.AssemblyVersion.ToString() + "\r\n" +
            "Last Modified:\r\n" + progVer.AssemblyVersion.ToDateTime().ToString("yy/MM/dd HH:mm") + "\r\n" +
            "※After the update\r\nIt will restart automatically";
        msgForm.SetSubMessage("Do you want to update?");
        var resp = msgForm.OKCancelShowDialog(true) == DialogResult.OK;
        if (resp)
        {
            CabFileUpdatePath = filename;
            UpdateApplication = true;
            Invoke((Action)(() => restartApplicationTimer.Enabled = true));
        }
        else
        {
            File.Delete(filename);
        }
    }
    finally
    {
        if (smsReceive1 != null) smsReceive1.ServicePaused = ServiceStatus.None;
    }
}

private bool WillUpdateVersion(SmartShooter.SmsService.CabinetVersion ver)
{
    var appVer = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
    if (ver.CompareToExe(appVer) > 0) return true;
    return false;
}

progVer.AssemblyVersion.ToDateTime().ToString("yy/MM/dd HH:mm")

最后,这是因为我在版本中使用了“*”。

public partial class MyVersion {
    
    public ushort Build { get; set; }
    
    public ushort Major { get; set; }
    
    public ushort Minor { get; set; }
    
    public ushort Revision { get; set; }
    

    public override string ToString()
    {
        return string.Format("{0}.{1}.{2}.{3}", Major, Minor, Build, Revision);
    }

    public DateTime ToDateTime()
    { 
        var dt = new DateTime(2000, 1, 1, 0, 0, 0);
        //plus days
        dt = dt.AddDays(Build);
        //plus seconds
        dt = dt.AddSeconds(Revision * 2);
        return dt;
    }
}

我使用“Build”值来查找项目编译的日期,并使用“Revision”来获取精确的时间。