How to Programmatically Get the Version of a Dll or Exe File

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.

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.

Get the file version of a dll or exe

Here's an example using sigcheck:

@ECHO OFF

FOR /F "tokens=1-3" %%i IN ('p:\supporttools\sigcheck.exe p:\supporttools\procmon.exe') DO ( IF "%%i %%j"=="File version:" SET filever=%%k )

ECHO The version of the file is %filever%

PAUSE

Programmatically get the ProductVersion of .exe using GetFileVersionInfo API (C++)

The version info resource contains a small fixed portion (VS_FIXEDFILEINFO) and optionally some strings.

Some applications display the numbers from the fixed portion and some use the FileVersion/ProductVersion strings.

You should probably use the string if it is present because it allows the developer to add extra pieces of information like Alpha/Beta etc. and because some people forget to properly set the correct version in the fixed part.

Use the VerQueryValue function to get a list of languages and the strings...

How to get .exe file version number from file path

You can use FileVersionInfo.FileVersion to fetch this from a path.

var versionInfo = FileVersionInfo.GetVersionInfo(pathToExe);
string version = versionInfo.FileVersion; // Will typically return "1.0.0.0" in your case

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



Related Topics



Leave a reply



Submit