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:
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
Getting Specified Node Values from Xml Document
What's the Difference Between Returning Void and Returning a Task
How to Get the Time Difference Between Two Datetime Objects Using C#
Implementing Collectionchanged
Access to Foreach Variable in Closure Warning
How to Use Xpath with Xdocument
Can't Get SQL Server Compact 3.5/4 to Work with Asp .Net MVC 2
How to Use Dependency Injection with an Attribute
Use Reflection to Invoke an Overridden Base Method
How to Find Out When You'Ve Been Loaded via Xml Serialization
Datetime Conversion from String C#
Why Use Try {} Finally {} with an Empty Try Block
Using ASP.NET Identity Database First Approach
Illustrating Usage of the Volatile Keyword in C#
What's the Difference Between Application.Run() and Form.Showdialog()