Marek Kwiendacz
Marek Kwiendacz

Reputation: 9864

How to get version of used 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)

Upvotes: 4

Views: 3145

Answers (2)

kyrylomyr
kyrylomyr

Reputation: 12652

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();

Upvotes: 7

Stecya
Stecya

Reputation: 23276

Try to use

        Assembly.GetCallingAssembly();
        Assembly.GetExecutingAssembly();

Upvotes: 3

Related Questions