问题
I want to determine the file version of dll file in 'c#' when the path is specified. Suppose path = "\x\y\z.dll" .
How to find the file version of z.dll when path is given?
NOTE: I use Compact Framework 3.5 SP1
回答1:
Normal Framework
If it is a .NET DLL you can use Reflection.
using System.Reflection;
Assembly assembly = Assembly.LoadFrom("\x\y\z.dll");
Version ver = assembly.GetName().Version;
If not, you can use System.Diagnostics:
using System.Diagnostics;
static string GetDllVersion(string dllPath)
{
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(dllPath);
return myFileVersionInfo.FileVersion;
}
// Sample invokation
string result = GetDllVersion(@"C:\Program Files (x86)\Google\Chrome\Application\20.0.1132.57\chrome.dll");
// result value **20.0.1132.57**
Compact Framework
If you are using .NET Compact Framework you don't have access to FileVersionInfo
You can check this stackoverflow question. In the unique answer you have a link to a blog with code that fixes your problem.
回答2:
// Get the file version for the notepad.
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);
From: http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.fileversion.aspx
So for you:
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(@"\x\y\z.dll");
This works if the dll is .net or Win32. Reflection methods only work if the dll is .net.
来源:https://stackoverflow.com/questions/11645143/how-to-determine-file-version-of-dll-file-in-compact-framework-3-5