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

StackOverflow https://stackoverflow.com/questions/17688660

  •  03-06-2022
  •  | 
  •  

سؤال

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?

هل كانت مفيدة؟

المحلول

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;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top