46

My application has an exe and uses some DLLs. I am writing all in C#.

In one DLL I want to write a method to get the application name and version from the version information in the exe.

I understand that in full .NET I could use GetEntryAssembly, but that that is unavailable in CF.

cja
  • 9,512
  • 21
  • 75
  • 129

5 Answers5

94

Getting the app name:

System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

Getting the version:

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

You might want to use GetCallingAssembly() or getting the assembly by type (e.g. typeof(Program).Assembly) if your DLL is trying to get the EXE version and you don't have immediate access to it.

EDIT

If you have a DLL and you need the name of the executable you have a few options, depending on the use case. You can get the Assembly from a type contained in the EXE assembly, but since it would be rare for the DLL to reference the EXE, it requires the EXE pass in an object of that type.

Version GetAssemblyVersionFromObjectType(object o)
{
    o.GetType().Assembly.GetName().Version;
}

You could also do a little bit of an end-run like this:

[DllImport("coredll.dll", SetLastError = true)]
private static extern int GetModuleFileName(IntPtr hModule, StringBuilder lpFilename, int nSize);

...

var name = new StringBuilder(1024);
GetModuleFileName(IntPtr.Zero, name, 1024);
var version = Assembly.LoadFrom(name.ToString()).GetName().Version;
ctacke
  • 66,480
  • 18
  • 94
  • 155
  • 1
    GetExecutingAssembly() will get the name of the DLL it is called from. I want the name of the exe. – cja Feb 13 '13 at 09:08
  • GetCallingAssembly() is more complicated and can have unexpected results. – cja Feb 13 '13 at 09:09
  • Please explain "getting the assembly by type (e.g. typeof(Program).Assembly)" – cja Feb 13 '13 at 09:09
26

System.Reflection.Assembly.GetEntryAssembly().GetName().Version;

This function will give version of Application from where other libraries are loaded.

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

This will give version of current library. If you call this in Application library you will get application version and if call this in a DLL then will get that DLL version.

So in my opinion System.Reflection.Assembly.GetEntryAssembly().GetName().Version; is currect function to use.

SteveC
  • 15,808
  • 23
  • 102
  • 173
Rohan D
  • 464
  • 6
  • 17
18

If you want to create a component (.dll) use into another app reference to fetch the main app name and version, you can use this way:

Get the main app name:

AppDomain.CurrentDomain.DomainManager.EntryAssembly.GetName().Name;

Get the version:

AppDomain.CurrentDomain.DomainManager.EntryAssembly.GetName().Version.ToString();    

Get the FullName (contain: app name, app version, culture, publicKeyToken):

AppDomain.CurrentDomain.DomainManager.EntryAssembly.FullName;

But in this solution there is a problem, that's dependent on the host and if it runs directly from the executable file, the error will occur. Therefore the following elections:

Get the main app name:

string appName = AppDomain.CurrentDomain.FriendlyName;
appName = appName.Substring(0, appName.IndexOf('.'));

Get the version:

System.Windows.Forms.Application.ProductVersion;
Behzad
  • 3,502
  • 4
  • 36
  • 63
-1

This worked for me

var versionInfo = FileVersionInfo.GetVersionInfo(AppDomain.CurrentDomain.BaseDirectory + AppDomain.CurrentDomain.FriendlyName);
string version = versionInfo.FileVersion;

This gets the version of the current running EXE, regardless if this string is called from a DLL/other project linked, etc

WiiLF
  • 304
  • 3
  • 11
-6

Can you try using FileInfo Class FileVersionInfo. Hope this will help..

You can also try Win32 API like GetModuleFile()

Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42