Programmatically Get the Version Number of a Dll

Programmatically get the version number of a DLL

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

Important:
It should be noted that this is not the best answer to the original question. Don't forget to read more on this page.

How to check the version of an assembly (dll)?

You can use Reflector, ILDASM or ILSpy to get the assembly version.

You usually can find ILDASM in C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\ildasm.exe (where v8.1A is the version of the Windows SDK installed).

ILDASM:

ildasm

Reflector:

reflector

How do I programmatically get the version of a DLL or EXE file?

You would use the GetFileVersionInfo API.

See Using Version Information on the MSDN site.

Sample:

DWORD  verHandle = 0;
UINT size = 0;
LPBYTE lpBuffer = NULL;
DWORD verSize = GetFileVersionInfoSize( szVersionFile, &verHandle);

if (verSize != NULL)
{
LPSTR verData = new char[verSize];

if (GetFileVersionInfo( szVersionFile, verHandle, verSize, verData))
{
if (VerQueryValue(verData,"\\",(VOID FAR* FAR*)&lpBuffer,&size))
{
if (size)
{
VS_FIXEDFILEINFO *verInfo = (VS_FIXEDFILEINFO *)lpBuffer;
if (verInfo->dwSignature == 0xfeef04bd)
{

// Doesn't matter if you are on 32 bit or 64 bit,
// DWORD is always 32 bits, so first two revision numbers
// come from dwFileVersionMS, last two come from dwFileVersionLS
TRACE( "File Version: %d.%d.%d.%d\n",
( verInfo->dwFileVersionMS >> 16 ) & 0xffff,
( verInfo->dwFileVersionMS >> 0 ) & 0xffff,
( verInfo->dwFileVersionLS >> 16 ) & 0xffff,
( verInfo->dwFileVersionLS >> 0 ) & 0xffff
);
}
}
}
}
delete[] verData;
}

Getting DLL Version from within the DLL Programmatically in C++ - Again

You are printing out the ProductVersion fields when you should be printing out the FileVersion fields instead:

printf("Version is %d.%d.%d.%d",
HIWORD(fileInfo->dwFileVersionMS),
LOWORD(fileInfo->dwFileVersionMS),
HIWORD(fileInfo->dwFileVersionLS),
LOWORD(fileInfo->dwFileVersionLS));

Also, your code is leaking the verInfo array if GetFileVersionInfo() or VerQueryValue() fail.

How to get a DLL to programmatically obtaint it's version?

Use GetExecutingAssembly, as MSDN states:

Gets the assembly that contains the code that is currently executing

so you can write:

Dim version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version

How to determine file version of dll file in Compact Framework 3.5

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.



Related Topics



Leave a reply



Submit