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;