2017-08-09 109 views
4

這個讓我頭疼。我們曾經把東西放在[assembly: AssemblyVersion("1.0.0.0")]下的項目屬性中,我對改變完全沒問題,所以我不在乎它在哪裏。我知道他們也會爲版本制定一個新的標準,也很好。如何手動設置組裝版本

大量的文件指向project.json文件,這顯然是一種浪費,因爲這不再是一個合法的文件。最近說以下內容添加到您的.csproj文件:

<PropertyGroup> 
    <VersionPrefix>1.2.3</VersionPrefix> 
    <VersionSuffix>alpha</VersionSuffix> 
</PropertyGroup> 

而且完全是浪費。只是因爲我似乎無法閱讀它。以下總是給我1.0.0.0

PlatformServices.Default.Application.ApplicationVersion 

更何況,當我在File Explorer單擊鼠標右鍵,然後單擊Properties,然後Details標籤還總是說1.0.0.0

那麼,我如何設置我的解決方案中的每個程序集的版本,然後在運行時稍後再讀取它們?

回答

0

原來這個開始工作的時候.Net Core 2.0出來了。當你在項目單擊鼠標右鍵,然後單擊屬性有它的UI,如下圖所示:

UI of Project Properties

Package VersionAssembly VersionAssembly File Version對應VersionAssemblyVersionFileVersion設置分別在.csproj文件中:

<PropertyGroup> 
    <Version>1.1.0</Version> 
    <AssemblyVersion>1.1.0.9</AssemblyVersion> 
    <FileVersion>1.1.0.9</FileVersion> 
</PropertyGroup> 

然後我創建了uti在做以下我的解決方案的每個項目lity方法:

public static VersionInformationModel GetVersionInformation() { 
    var Result = new VersionInformationModel { 
    Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(), 
    BuildDate = System.IO.File.GetLastWriteTime(Assembly.GetExecutingAssembly().Location), 
    Configuration = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyConfigurationAttribute>().Configuration, 
    TargetFramework = Assembly.GetExecutingAssembly().GetCustomAttribute<System.Runtime.Versioning.TargetFrameworkAttribute>().FrameworkName, 
    }; 

    return Result; 
} 

所以我的網站的管理頁面上,我可以知道什麼時候的版本和其他詳情有關每個項目,因爲它站在我期待在任何服務器。

3

上的方法是設置這些值的項目中的文件:

<PropertyGroup> 
    <Version>1.3.5.7</Version> 
    <FileVersion>2.4.6.8</FileVersion> 
</PropertyGroup> 

而且這樣的閱讀:

var fileVersion = Assembly.GetEntryAssembly() 
    .GetCustomAttribute<AssemblyFileVersionAttribute>() 
    .Version; 

var informationalVersion = Assembly.GetEntryAssembly() 
    .GetCustomAttribute<AssemblyInformationalVersionAttribute>() 
    .InformationalVersion; 

值得一提的是,設置在.csproj的文件,這些價值自動生成(所以不要試圖編輯它)類似於傳統的文件AssemblyInfo.cs包含這樣的內容:

//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated by a tool. 
//  Runtime Version:4.0.30319.42000 
// 
//  Changes to this file may cause incorrect behavior and will be lost if 
//  the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

using System; 
using System.Reflection; 

//snip  
[assembly: System.Reflection.AssemblyFileVersionAttribute("2.4.6.8")] 
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.2.3")] 

// Generated by the MSBuild WriteCodeFragment class. 
+0

除非我遺漏了一些東西,否則這是行不通的。我把''作爲第二個''放在''TargetFramework'的一個'netcoreapp1.1'之後,'fileVersion'和'informationalVersion'都回來了'1.0.0.0',文件Explorer |屬性|細節還說'1.0.0.0'。我在一些錯誤的版本或.Net核心或什麼?啊! – Grandizer

+0

那麼你不需要一個新的屬性組,只需將這些值與目標框架。 – DavidG

+0

將版本移到第一個「PropertyGroup」中並沒有改變。我明白了不會更改自動生成的文件,但我什至不會在任何地方看到AssemblyInfo.cs文件。即使在重建之後。我肯定錯過了什麼? – Grandizer