Extract executable assembly/version information

Methods we are going to use:

GetFileVersionInfo Retrieves version information for the specified file.

GetFileVersionInfoSize Determines whether the operating system can retrieve version information for a specified file. If version information is available, GetFileVersionInfoSize returns the size, in bytes, of that information.

VerQueryValue Retrieves specified version information from the specified version-information resource. To retrieve the appropriate resource, before you call VerQueryValue, you must first call the GetFileVersionInfoSize function, and then the GetFileVersionInfo function.

Code:

[DllImport("version.dll")]
public static extern bool GetFileVersionInfo(string sFileName,
	  uint handle, uint size, IntPtr infoBuffer);
 
[DllImport("version.dll")]
public static extern int GetFileVersionInfoSize(string sFileName,
	  out uint handle);
 
[DllImport("version.dll")]
unsafe public static extern bool VerQueryValue(IntPtr pBlock,
	  string pSubBlock, out IntPtr pValue, out uint len);
 
void GetExeInfo(String f)
{
    try
    {
       if (!File.Exists(f)) return;
       uint handle = 0;
 
       uint size = (uint)GetFileVersionInfoSize(f, out handle);
       if (size > 0)
       {
	    IntPtr buffer = System.Runtime.InteropServices.Marshal.AllocHGlobal((int)(size));
 
	    if (GetFileVersionInfo(f, handle, size, buffer))
	    {
		uint len = 0;
		IntPtr Info = IntPtr.Zero;
 
		if (VerQueryValue(buffer, @"VarFileInfoTranslation", out Info, out len))
		{
			short wLang = System.Runtime.InteropServices.Marshal.ReadInt16(Info, 0);
			short wCp = System.Runtime.InteropServices.Marshal.ReadInt16(Info, 2);
 
			if (VerQueryValue(buffer, @"StringFileInfo" + (wLang).ToString("X4") + (wCp).ToString("X4") + @"CompanyName", out Info, out len))
				Console.WriteLine(Marshal.PtrToStringAnsi(Info));
 
			if (VerQueryValue(buffer, @"StringFileInfo" + (wLang).ToString("X4") + (wCp).ToString("X4") + @"FileDescription", out Info, out len))
				Console.WriteLine(Marshal.PtrToStringAnsi(Info));
 
			if (VerQueryValue(buffer, @"StringFileInfo" + (wLang).ToString("X4") + (wCp).ToString("X4") + @"FileVersion", out Info, out len))
				Console.WriteLine(Marshal.PtrToStringAnsi(Info));
 
			if (VerQueryValue(buffer, @"StringFileInfo" + (wLang).ToString("X4") + (wCp).ToString("X4") + @"InternalName", out Info, out len))
				Console.WriteLine(Marshal.PtrToStringAnsi(Info));
 
			if (VerQueryValue(buffer, @"StringFileInfo" + (wLang).ToString("X4") + (wCp).ToString("X4") + @"LegalCopyright", out Info, out len))
				Console.WriteLine(Marshal.PtrToStringAnsi(Info));
 
			if (VerQueryValue(buffer, @"StringFileInfo" + (wLang).ToString("X4") + (wCp).ToString("X4") + @"LegalTrademarks", out Info, out len))
				Console.WriteLine(Marshal.PtrToStringAnsi(Info));
 
			if (VerQueryValue(buffer, @"StringFileInfo" + (wLang).ToString("X4") + (wCp).ToString("X4") + @"OriginalFileName", out Info, out len))
				Console.WriteLine(Marshal.PtrToStringAnsi(Info));
 
			if (VerQueryValue(buffer, @"StringFileInfo" + (wLang).ToString("X4") + (wCp).ToString("X4") + @"ProductName", out Info, out len))
				Console.WriteLine(Marshal.PtrToStringAnsi(Info));
 
			if (VerQueryValue(buffer, @"StringFileInfo" + (wLang).ToString("X4") + (wCp).ToString("X4") + @"ProductVersion", out Info, out len))
				Console.WriteLine(Marshal.PtrToStringAnsi(Info));
 
			if (VerQueryValue(buffer, @"StringFileInfo" + (wLang).ToString("X4") + (wCp).ToString("X4") + @"Comments", out Info, out len))
				Console.WriteLine(Marshal.PtrToStringAnsi(Info));
 
			if (VerQueryValue(buffer, @"StringFileInfo" + (wLang).ToString("X4") + (wCp).ToString("X4") + @"PrivateBuild", out Info, out len))
				Console.WriteLine(Marshal.PtrToStringAnsi(Info));
 
			if (VerQueryValue(buffer, @"StringFileInfo" + (wLang).ToString("X4") + (wCp).ToString("X4") + @"SpecialBuild", out Info, out len))
				Console.WriteLine(Marshal.PtrToStringAnsi(Info));
	        }
    	    }
	}
    }
    catch { }
}
%d bloggers like this: