Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get version of used dll

Tags:

c#

.net

dll

I'm working on set of webparts that uses common library.
To testing deployment I need to add version info in generated html. Method that add version "watermark" to page is in common library.

So I have something like this (it is more complicated, because in common library is base class for webparts, but for this problem we can simplify it):

In control from mainAssembly.dll I'm calling OnInit method:

protected override void OnInit(EventArgs e)  
{  
..  
    Library.AddWatermark(this);  
..  
}

and in common library I have:

public void AddWatermark(Control ctrl)  
{    
    string assemblyVersion = GetAssemblyVersion();  
    ctrl.Controls.Add(new HiddenField { Value = string.Format("Version: {0}",   assemblyVersion ) });  
}  

So my question is: how to get version of assembly when we are in method from this assembly? (in AddWatermark)? And if it is possible to get version of caller assembly? (mainAssembly)

like image 342
Marek Kwiendacz Avatar asked Dec 29 '22 00:12

Marek Kwiendacz


2 Answers

Version of caller assembly:

Assembly assem = Assembly.GetCallingAssembly();
AssemblyName assemName = assem.GetName();
Console.WriteLine(assemName.Version.Major);
Console.WriteLine(assemName.Version.Minor);

To get version of current assembly replace first line of code with

Assembly assem = Assembly.GetExecutingAssembly();
like image 50
kyrylomyr Avatar answered Dec 30 '22 15:12

kyrylomyr


Try to use

        Assembly.GetCallingAssembly();
        Assembly.GetExecutingAssembly();
like image 39
Stecya Avatar answered Dec 30 '22 13:12

Stecya