0

I want to find the dll version when the path is specified. Suppose path = "progfiles/harry/sample.dll". How to find the sample.dll version. Since I am using .net framework 3.5 SP1, I cannot use FileVersionInfo. I tried Assembly.LoadFrom. But the problem I am facing with LoadFrom is "If an assembly with the same identity is already loaded, LoadFrom returns the loaded assembly even if a different path was specified."

yms
  • 10,361
  • 3
  • 38
  • 68

3 Answers3

1

Anyway, you could just load your assembly into another AppDomain.

var domain = AppDomain.CreateDomain("tmp");
var version = domain.Load().GetName(path).Version;

EDIT:

You are targeting Windows CE, so can use the GetFileVersionInfo function.

Here is a full code sample how to use this function from within .Net/C#.

sloth
  • 99,095
  • 21
  • 171
  • 219
  • I could not find Load() function in domain? Should I add something? – Badhri Ravikumar Jul 27 '12 at 10:16
  • What do mean you *could not find* it. Do you get a compiler error? [It is there...](http://msdn.microsoft.com/en-us/library/dwzhhcfk) – sloth Jul 27 '12 at 10:27
  • Error - 'System.AppDomain' does not contain a definition for 'Load' and no extension method 'Load' accepting a first argument of type 'System.AppDomain' could be found (are you missing a using directive or an assembly reference?) I am getting this error. – Badhri Ravikumar Jul 27 '12 at 10:35
  • Which version of Visual Studio do you use and which `Target Framework` is set in your project setting? – sloth Jul 27 '12 at 10:38
  • Target Framework is Windows CE and I am using Visual Studio 2008 version 9.0.30729.1 SP and .net framework is version 3.5 SP1 – Badhri Ravikumar Jul 27 '12 at 10:44
0

Example code to find version of DLL library:

Version ver = Assembly.LoadFrom("Library.dll").GetName().Version;

Edit 1: OK, for getting already executing assembly you can try this example:

Assembly SampleAssembly;
SampleAssembly = Assembly.GetAssembly(ObjectLoadedFromDLL.GetType());
Version ver = Assembly.GetExecutingAssembly().GetName().Version;

And links to MSDN this full documentation of this method: Assembly.GetExecutingAssembly Method

vchyzhevskyi
  • 753
  • 1
  • 11
  • 20
  • The OP had specific issues with this method; the inability to load a DLL that has the same identity as an already loaded one. – Mr47 Jul 27 '12 at 09:57
  • 1
    @coirius - I think you didnt understand my question. I said "If an assembly with the same identity is already loaded, LoadFrom returns the loaded assembly even if a different path was specified." I want to know the solution in this case – Badhri Ravikumar Jul 27 '12 at 09:59
  • @coirius - If suppose, sample.dll is already loaded once and I have another dll file with the same name at the location path = "progfiles/harry/sample.dll". Now I want dll file version located at path. But whenever I try to load sample.dll, by default it is taking the previously loaded one. Check the first disadvantage here http://msdn.microsoft.com/en-us/library/1009fa28.aspx – Badhri Ravikumar Jul 27 '12 at 10:26
0

You can use AssemblyName :

var assemblyName = AssemblyName.GetAssemblyName(assemblyPath);
System.Diagnostics.Debug.WriteLine(assemblyName.Version);
Seb
  • 2,637
  • 1
  • 17
  • 15