Question

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

Was it helpful?

Solution

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.

OTHER TIPS

// 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top