using System; using System.Collections.Generic; using System.Runtime.InteropServices; public class VersionInfo { [DllImport("version.dll")] private static extern bool GetFileVersionInfo(string sFileName, int handle, int size, byte[] infoBuffer); [DllImport("version.dll")] private static extern int GetFileVersionInfoSize (string sFileName, out int handle); [DllImport("version.dll")] private static extern bool VerQueryValue (byte[] pBlock, string pSubBlock, out string pValue, out uint len); [DllImport("version.dll")] private static extern bool VerQueryValue (byte[] pBlock, string pSubBlock, out IntPtr pValue, out uint len); private static string[] keys = { "Comments", "InternalName", "ProductName", "CompanyName", "LegalCopyright", "ProductVersion", "FileDescription", "LegalTrademarks", "PrivateBuild", "FileVersion", "OriginalFilename", "SpecialBuild" }; public static Dictionary Dump(string fnm) { Dictionary res = new Dictionary(); int handle; int size = GetFileVersionInfoSize(fnm, out handle); if(size <= 0) return res; byte[] buffer = new byte[size]; if(!GetFileVersionInfo(fnm, handle, size, buffer)) return res; IntPtr subblock; uint len; if(!VerQueryValue(buffer, @"\VarFileInfo\Translation", out subblock, out len)) return res; foreach(string key in keys) { string spv = @"\StringFileInfo\" + Marshal.ReadInt16(subblock, 0).ToString("X4") + Marshal.ReadInt16(subblock, 2).ToString("X4") + @"\" + key; string val; if(VerQueryValue(buffer, spv, out val, out len)) { res.Add(key, val); } } return res; } } public class C { public static void Main (string[] args) { Dictionary ver = VersionInfo.Dump(@"C:\WINDOWS\system32\kernel32.dll"); Console.WriteLine(ver["ProductName"]); Console.WriteLine(ver["CompanyName"]); Console.WriteLine(ver["ProductVersion"]); Console.WriteLine(ver["FileVersion"]); } }