最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】C#中获得当前项目的程序集中的文件版本信息

C# crifan 4899浏览 0评论

【问题】

当前有个C#的项目,想要获得对应的该项目中的程序集:

project property assembly

中的文件版本信息,即那个:文件版本: 4 3 0 0

中的主版本号4和次版本号3。

【解决过程】

1.网上搜了一堆资料,最后还是这里解释的清楚:

C# AssemblyFileVersion usage within a program

然后自己去添加了对应的代码后,结果却提示找不到引用:

未能找到类型或命名空间名称 Assembly

最后

参考:Assembly.GetFile Method,才知道对于Assembly涉及的Namespace是System.Reflection

参考:FileVersionInfo.GetVersionInfo Method,才知道对于FileVersionInfo涉及的Namespace是System.Diagnostics

 

所以,再添加了两个相应的using,最后的代码如下:

using System.Diagnostics;
using System.Reflection;

private void fileVersionInfoTest()
{
    Assembly asm = Assembly.GetExecutingAssembly();
    FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
    string versionStr = String.Format("{0}.{1}", fvi.ProductMajorPart, fvi.ProductMinorPart);
}

最终,就可以实现需要的功能了。

详细调试时的截图如下:

assembly info

 

file version info

 

get major minor version

 

【总结】

想要获得当前C#项目的程序集的文件版本,可以用如下代码实现:

using System.Diagnostics;
using System.Reflection;

private void fileVersionInfoTest()
{
    Assembly asm = Assembly.GetExecutingAssembly();
    FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
    string versionStr = String.Format("{0}.{1}", fvi.ProductMajorPart, fvi.ProductMinorPart);
}

转载请注明:在路上 » 【已解决】C#中获得当前项目的程序集中的文件版本信息

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.171 seconds, using 22.13MB memory