3

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

Oscar Foley
  • 6,817
  • 8
  • 57
  • 90

2 Answers2

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

weston
  • 54,145
  • 21
  • 145
  • 203
  • 1
    FileVersionInfo is not available in CompactFramework. So this answer is not helpful. – etalon11 Feb 15 '16 at 10:05
  • @etalon11 You are correct. In my defence, when I answered the question was **not** about compact framework, it was edited several hours afterwards to include that tag and description. – weston Feb 15 '16 at 13:58
  • @etalon11 I'm reluctant to remove or edit though, as so many people have found this answer useful. – weston Feb 15 '16 at 14:00
  • Right. You could add my hint to your question, so it will help others not to trap into a wrong direction. – etalon11 Feb 15 '16 at 14:22
2

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.

Community
  • 1
  • 1
Oscar Foley
  • 6,817
  • 8
  • 57
  • 90
  • And what happens when the dll is native? Also you have now loaded an assembly into your process. – leppie Jul 25 '12 at 08:05
  • Good point, I edited my question to clarify is only valid in the case of .NET dll – Oscar Foley Jul 25 '12 at 08:08
  • @cad if suppose those are not .NET dll, then what should I do? – Badhri Ravikumar Jul 25 '12 at 08:11
  • I have edited answer including a function you can use for any kind of dll – Oscar Foley Jul 25 '12 at 08:14
  • @cad I have added both System.Reflection and System.Diagnostics in my project. But I could not use FileVersionInfo. I am using .net framework 3.5 SP1. What might be the problem? – Badhri Ravikumar Jul 25 '12 at 08:19
  • Going to check. I tested it in my local using .net Framework 4.0. Give me 5 mins :) – Oscar Foley Jul 25 '12 at 08:29
  • I have tested it with .net framework 3.5 and works. As you can see in [MSDN](http://msdn.microsoft.com/en-us/library/system.diagnostics%28v=vs.90%29.aspx) you should have [FileVersion](http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo%28v=vs.90%29) – Oscar Foley Jul 25 '12 at 08:33
  • @cad - FileVersionInfo is supported for .net framework 3.5 but mine is 3.5 SP1. Does both make any difference? – Badhri Ravikumar Jul 25 '12 at 08:43
  • @Badhri it shouldn't be different... – Oscar Foley Jul 25 '12 at 08:50
  • @cad - when I enter using System.Diagnostics. it displays only CodeAnalysis. If FileVersion is supported in .net framework 3.5, then when I enter System.Diagnostics. ,FileVersionInfo must be displayed. So I dont think so this is supported. Do I have any other method? – Badhri Ravikumar Jul 25 '12 at 08:57
  • In theory .net framework 3.5 sp1 is 3.5 plus some fixes. You should have it. Could you add more information about your project? Do you use compact framework? Which kind of project? What references do you have in the project? (I have Microsoft.Csharp, System, System.Core, System.Data, System.Data.DataSetExtensions, System.Xml, System.Xml.Linq) – Oscar Foley Jul 25 '12 at 09:40
  • @cad I am using compact framework. It is a windows mobile application. The references I am using are using System; using System.Collections.Generic,using System.Text,using System.IO, using System.Net,using System.Reflection,using System.Xml,using System.Windows.Forms,using System.Diagnostics,using Ionic.Zip,using System.Threading,using System.Runtime.InteropServices,using System.Linq,using System.ComponentModel,using System.Data,using System.Drawing. here in my project, i need to compare the file version of two dll files. – Badhri Ravikumar Jul 25 '12 at 09:56
  • 1
    Compact Framework is the problem. I have updated my question to point you to a solution. – Oscar Foley Jul 25 '12 at 10:05
  • 1
    You're welcome. I have edited your question to highlight that you are asking about Compact Framework. – Oscar Foley Jul 25 '12 at 11:14
  • @cad - I am getting one problem. "If an assembly with the same identity is already loaded, LoadFrom returns the loaded assembly even if a different path was specified.". I thought of trying LoadFile. But it is not available for me. I am using .net framework 3.5 SP1. Do I have any other option? – Badhri Ravikumar Jul 27 '12 at 06:50