How to determine file version of dll file in Compact Framework 3.5

3.2k views Asked by At

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

2

There are 2 answers

15
Oscar Foley On BEST ANSWER

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.

4
weston On
// 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.