Getting the program version of an executable file like explorer does in C# .NET 2 [duplicate]

守給你的承諾、 提交于 2019-12-02 13:19:36
deegee

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

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

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.

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.

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.

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());

Or just:

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

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

To retrieve this information for your executing exe you use:

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

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

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

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!