How to get .exe file version number from file path

2019-01-22 16:35发布

I am using .Net 3.5/4.0 with code in C#.

I am trying to get a version number of an exe file on my C: drive.

For example path is: c:\Program\demo.exe. If the version number of demo.exe is 1.0.

How can i use this path to grab version number?.

7条回答
等我变得足够好
2楼-- · 2019-01-22 17:14

In the accepted answer a reference is made to "pathToExe".

This path can be retrieved and used as follows:

var assembly = Assembly.GetExecutingAssembly();
var fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
var version = fvi.FileVersion; // or fvi.ProductVersion

Hope this saves someone from doing some unnecessary extra steps.

查看更多
Deceive 欺骗
3楼-- · 2019-01-22 17:15

Use, it work:

using System.Reflection;

string v = AssemblyName.GetAssemblyName("Path/filename.exe").Version.ToString();
查看更多
forever°为你锁心
4楼-- · 2019-01-22 17:23

Where Program is your class name:

Console.WriteLine("Version = " + typeof(Program).Assembly.GetName().Version.ToString()) ;
查看更多
来,给爷笑一个
5楼-- · 2019-01-22 17:23
//Example your file version is 1.0.0.0
//Solution 1
Dim fileVer As FileVersionInfo = FileVersionInfo.GetVersionInfo(Environment.CurrentDirectory + "\yourExe.exe")
yourLabel.Text = fileVer.FileVersion
//Solution 2
//Get File Version Number
yourLabel.Text = Application.ProductVersion
//Both solution will get output 1.0.0.0
查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-22 17:24

You can use FileVersionInfo.ProductVersion to fetch this from a path.

var versionInfo = FileVersionInfo.GetVersionInfo(pathToExe);
string version = versionInfo.ProductVersion; // Will typically return "1.0.0" in your case
查看更多
冷血范
7楼-- · 2019-01-22 17:31

I'm not sure if this is what you are looking for, but:

http://www.daniweb.com/software-development/csharp/threads/276174/c-code-to-get-dll-version

It says

int i;

// Get the file version for the notepad.
FileVersionInfo.GetVersionInfo(Path.Combine(Environment.SystemDirectory, "notepad.exe"));

FileVersionInfo myFileVersionInfo =  FileVersionInfo.GetVersionInfo(Environment.SystemDirectory + "\\notepad.exe");

// Print the file name and version number.
Console.WriteLine("File: " + myFileVersionInfo.FileDescription + '\n' + "Version number: " + myFileVersionInfo.FileVersion);
查看更多
登录 后发表回答