在C#.NET 2中获取像Explorer这样的可执行文件的程序版本 [英] Getting the program version of an executable file like explorer does in C# .NET 2

查看:92
本文介绍了在C#.NET 2中获取像Explorer这样的可执行文件的程序版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在资源管理器中的可执行文件上单击鼠标右键,然后选择属性。然后在属性对话框中,选择版本标签。然后,我单击productversion。这是我需要用c#获得的值。
我用 fileversioninfo.productversion尝试过,但是如果我进入资源管理器 1.85,fileversioninfo会返回奇怪的值,例如:1,00000,8,00。点变成逗号,最后一位被去除。如果重要的话,我会在赢7 64位上使用net 2。
fileversioninfo不会返回我期望的结果或资源管理器会返回的结果。
我也尝试过 fileversioninfo.fileversion,但它以某种方式返回的值与fileversioninfo.productversion一样完全相同。在资源管理器视图中,原始文件属性对话框窗口中的文件版本和产品版本之间有明显的区别。
.Net及其fileversioninfo类不会以资源管理器那样显示值。
我该怎么做才能以资源管理器方式获取版本的值?

if I make a right-click in explorer on an executable file and choose "properties". then in the properties dialog I select the tab "version". Then I click on the productversion. This is the value which I need to get with c#. I tried it with "fileversioninfo.productversion", but if I get in the explorer "1.85" the fileversioninfo returns me weird values like: 1,00000,8,00. Dots become commas and the last digits are stripped off. I use net 2 on win 7 64 bit, if that matters. fileversioninfo doesn't return what I would expect or what explorer does. I tried also "fileversioninfo.fileversion", but it somehow returns even exact the same weird value like the fileversioninfo.productversion. In explorer view there is a clear difference between fileversion and productversion in the original fileproperties dialog window. .Net with its fileversioninfo class doesn't display the values in that way how explorer would. What must I do to get the values of the version in the explorer way?

推荐答案

重新阅读您的帖子,要获取另一个exe的文件版本还是您的exe?

Re-reading your post, you want to get the file version of another exe or your exe?

对于另一个exe,请使用 FileVersionInfo ' s FileMajorPart FileMinorPart FileBuildPart FilePrivatePart ProductMajorPart ProductMinorPart ProductBuildPart ProductPrivatePart ,然后根据需要设置格式。

For another exe, use FileVersionInfo's FileMajorPart, FileMinorPart, FileBuildPart, FilePrivatePart, or ProductMajorPart, ProductMinorPart, ProductBuildPart, ProductPrivatePart, then format the values however you wish.

请注意,取决于公司或编译有问题的exe的人使用了哪个版本字段,它可能包含也可能不包含某些版本字段。

Be aware that depending on what version fields were used by the company or person who compiled the exe in question, it may or may not contain certain version fields.

如果使用 FileVersion ProductVersion 属性,它将返回一个64位数字,其中64位long中的四个16位short是 major.minor.build.private 组件。

If you use the FileVersion or ProductVersion property, it returns a 64-bit number where the four 16-bit shorts in the 64-bit long are the major.minor.build.private components.

但是,t如果您使用 .ToString(),则其格式应正确:

However, this should format correctly if you use the .ToString() so:

FileVersionInfo fvi = FileVersionInfo.GetVersionInfo("C:\\MyFile.txt");
string = fvi.ProductVersion.ToString();

如果格式不正确,请使用版本信息的每个单独组成部分。

If it does not format correctly, then use each individual component of the version information.

要显示另一个exe的格式化版本:

To display the formatted version of another exe:

FileVersionInfo fvi = FileVersionInfo.GetVersionInfo("C:\\MyFile.txt");

int major = fvi.ProductMajorPart;
int minor = fvi.ProductMinorPart;
int build = fvi.ProductBuildPart;
int revsn = fvi.ProductPrivatePart;

string = String.Concat(major.ToString(), ".", minor.ToString(), ".", build.ToString(), ".", revsn.ToString());

或者只是:

FileVersionInfo fvi = FileVersionInfo.GetVersionInfo("C:\\MyFile.txt");

string = String.Concat(fvi.ProductMajorPart.ToString(), ".", fvi.ProductMinorPart.ToString(), ".", fvi.ProductBuildPart.ToString(), ".", fvi.ProductPrivatePart.ToString());






要为执行的exe检索此信息,请使用:


To retrieve this information for your executing exe you use:

version = Assembly.GetExecutingAssembly().GetName().Version;

以Mmbr格式显示为字符串,使用:

Display as a string in the format M.m.b.r using:

string = Assembly.GetExecutingAssembly().GetName().Version.ToString();

哪个是完整版本的major.minor.build.revision,可以使用以下内容作为其组成部分进行检索:

Which is the full version major.minor.build.revision which can be retrieved as its component parts with:

major = Assembly.GetExecutingAssembly().GetName().Version.Major;
minor = Assembly.GetExecutingAssembly().GetName().Version.Minor;
build = Assembly.GetExecutingAssembly().GetName().Version.Build;
revision = Assembly.GetExecutingAssembly().GetName().Version.Revision;

这篇关于在C#.NET 2中获取像Explorer这样的可执行文件的程序版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆