51

它在 C# 项目的 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 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

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

4

5 回答 5

71

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

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

或者:

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

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

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

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

于 2012-04-19T14:01:51.600 回答
31
[assembly: AssemblyVersion("1.0.*")] 
//[assembly: AssemblyFileVersion("1.0.*")] 

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

于 2012-04-19T14:17:45.533 回答
7

那么为什么提供的评论说

// 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. :)
于 2019-03-26T00:02:34.830 回答
6

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

[程序集:AssemblyVersion("1.2.3.*")]

在中使用 '*'AssemblyVersion很好,但是按照我们应该使用版本结构*中的部分的 seekver.org 进行操作)。revision<major version>.<minor version>.<build number>.<revision>

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

进行不兼容的 API 更改时的主要版本,

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

进行向后兼容的错误修复时的补丁版本。

于 2015-07-24T11:19:01.110 回答
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”来获取精确的时间。

于 2021-01-19T03:17:54.420 回答