【问题标题】:Specifying assembly version in .fsproj for .NET core在 .fsproj 中为 .NET 核心指定程序集版本
【发布时间】:2026-02-17 07:00:01
【问题描述】:

我正在尝试在 .NET 核心 (2.0) 控制台应用程序中指定程序集版本,以便我可以通过以下方式以编程方式访问它:

open System.Reflection

let main argv =
    printfn "Assembly Version is: %s" <| Assembly.GetEntryAssembly().GetName().Version.ToString()
    0

将版本字段添加到我的 .fsproj 文件的属性组,例如:

 <PropertyGroup>
   <OutputType>Exe</OutputType>
   <TargetFramework>netcoreapp2.0</TargetFramework>
   <Version>1.0.0.1</Version>
 </PropertyGroup>

不会更改我的测试应用程序打印的版本(它保持在 0.0.0.0)。

有效的方法是添加一个 AssemblyInfo.fs 文件,其中设置了 AssemblyVersion 属性,但如果可能的话,我想避免这种情况并使用 .fsproj 文件。这可能吗?

我也很乐意有一个指针,指向我可以在哪里找到有关 .fsproj 的一般文档。

【问题讨论】:

    标签: f# .net-core


    【解决方案1】:

    .fsproj 文件的文档

    就 MSBuild 而言,我相信格式与 csproj 相同

    您可以在此处找到有关 csproj 文件的官方文档:
    https://docs.microsoft.com/en-us/dotnet/core/tools/csproj


    程序集版本号

    要获取程序集的“版本”,您应该注意有几种类型的版本:

    • AssemblyVersion:
      采用major.minor.build.revision 格式的数值(例如,2.4.0.0)。公共语言运行时使用此值在强名称程序集中执行绑定操作。
      注意:如果未将 AssemblyInformationalVersionAttribute 属性应用于程序集,则由 @987654332 指定的版本号@ 属性由 Application.ProductVersionApplication.UserAppDataPathApplication.UserAppDataRegistry 属性使用。

    • AssemblyFileVersion:
      指定 Win32 文件版本号的字符串值。这通常默认为程序集版本。

    • AssemblyInformationalVersion:
      指定公共语言运行时不使用的版本信息的字符串值,例如完整的产品版本号。
      注意:如果将此属性应用于程序集,则它指定的字符串可以是通过使用Application.ProductVersion 属性在运行时获得。该字符串还用于Application.UserAppDataPathApplication.UserAppDataRegistry 属性提供的路径和注册表项。

    • Application.ProductVersion:
      定义程序集清单的附加版本信息。

    您可以在official Microsoft Docs here 上更详细地了解每一项——或者您可以阅读assemblies in general here


    从引用的程序集中获取不同的版本

    // assembly version
    Assembly.GetExecutingAssembly().GetName().Version.ToString();
    
    // assembly version - by path
    Assembly.LoadFile('your assembly file').GetName().Version.ToString();
    
    // file version  **this is likely what you are seeking**
    FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion;
    
    // product version
    FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion;
    

    注意上面的代码sn-p取自an answer to a similar SO question


    直接解析.fsproj文件

    还可以选择仅使用 XML 解析 fsproj 文件。此选项旨在以编程方式添加引用或仅检查文件 - 因此它可能不适用于您的问题,但在这里是为了回答的完整性。

    //using System.Xml.Linq
    
    XDocument.Load(path).Descendants("PropertyGroup").Elements("Version").Single().Value;
    

    【讨论】:

    • 您的输出 (bin) 文件夹中没有 fsproj 文件,因此您无法解析它。
    • @IvanMilosavljevic - 我可能误读了 OP,尽管他想解析文件。我相应地更新了我的答案,感谢您的推动。
    • 感谢您的回答,遗憾的是在我的 .fsproj 文件中设置 AssemblyVersion 属性对调用 Assembly.GetExecutingAssembly().GetName() .Version.ToString() 时获得的值没有影响(我总是得到“0.0.0.0”)...我想这是一个错误呢?
    • @Svek FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly‌​().Location).FileVer‌​sion,只返回一个空字符串(在我的 .fsproj 文件中,我为 AssemblyVersionFileVersionVersion 设置了属性)
    【解决方案2】:

    这里有一个问题:

    Generate F# assemblyinfo in new fsproj

    简而言之,它应该可以工作,但现在不行。

    【讨论】:

    • 现在可与 .net sdk 2.2.100 或更高版本一起使用