// // This function tries to find version informaiton for a specific codepage. // Returns true when version information is found. // private bool GetVersionInfoForCodePage(IntPtr memIntPtr, string codepage) { string template = "\\\\StringFileInfo\\\\{0}\\\\{1}"; companyName = GetFileVersionString(memIntPtr, string.Format(CultureInfo.InvariantCulture, template, codepage, "CompanyName")); fileDescription = GetFileVersionString(memIntPtr, string.Format(CultureInfo.InvariantCulture, template, codepage, "FileDescription")); fileVersion = GetFileVersionString(memIntPtr, string.Format(CultureInfo.InvariantCulture, template, codepage, "FileVersion")); internalName = GetFileVersionString(memIntPtr, string.Format(CultureInfo.InvariantCulture, template, codepage, "InternalName")); legalCopyright = GetFileVersionString(memIntPtr, string.Format(CultureInfo.InvariantCulture, template, codepage, "LegalCopyright")); originalFilename = GetFileVersionString(memIntPtr, string.Format(CultureInfo.InvariantCulture, template, codepage, "OriginalFilename")); productName = GetFileVersionString(memIntPtr, string.Format(CultureInfo.InvariantCulture, template, codepage, "ProductName")); productVersion = GetFileVersionString(memIntPtr, string.Format(CultureInfo.InvariantCulture, template, codepage, "ProductVersion")); comments = GetFileVersionString(memIntPtr, string.Format(CultureInfo.InvariantCulture, template, codepage, "Comments")); legalTrademarks = GetFileVersionString(memIntPtr, string.Format(CultureInfo.InvariantCulture, template, codepage, "LegalTrademarks")); privateBuild = GetFileVersionString(memIntPtr, string.Format(CultureInfo.InvariantCulture, template, codepage, "PrivateBuild")); specialBuild = GetFileVersionString(memIntPtr, string.Format(CultureInfo.InvariantCulture, template, codepage, "SpecialBuild")); language = GetFileVersionLanguage(memIntPtr); NativeMethods.VS_FIXEDFILEINFO ffi = GetFixedFileInfo(memIntPtr); fileMajor = HIWORD(ffi.dwFileVersionMS); fileMinor = LOWORD(ffi.dwFileVersionMS); fileBuild = HIWORD(ffi.dwFileVersionLS); filePrivate = LOWORD(ffi.dwFileVersionLS); productMajor = HIWORD(ffi.dwProductVersionMS); productMinor = LOWORD(ffi.dwProductVersionMS); productBuild = HIWORD(ffi.dwProductVersionLS); productPrivate = LOWORD(ffi.dwProductVersionLS); fileFlags = ffi.dwFileFlags; // fileVersion is chosen based on best guess. Other fields can be used if appropriate. return(fileVersion != string.Empty); }
private static NativeMethods.VS_FIXEDFILEINFO GetFixedFileInfo(IntPtr memPtr) { IntPtr memRef = IntPtr.Zero; int memLen; if (UnsafeNativeMethods.VerQueryValue(new HandleRef(null, memPtr), "\\", ref memRef, out memLen)) { NativeMethods.VS_FIXEDFILEINFO fixedFileInfo = new NativeMethods.VS_FIXEDFILEINFO(); Marshal.PtrToStructure(memRef, fixedFileInfo); return(fixedFileInfo); } return(new NativeMethods.VS_FIXEDFILEINFO()); }
/// <include file='doc\FileVersionInfo.uex' path='docs/doc[@for="FileVersionInfo.GetVersionInfo"]/*' /> /// <devdoc> /// <para>Returns a System.Windows.Forms.FileVersionInfo representing the version information associated with the specified file.</para> /// </devdoc> public static FileVersionInfo GetVersionInfo(string fileName) { string fullPath; FileIOPermission fiop = new FileIOPermission(PermissionState.None); fiop.AllFiles = FileIOPermissionAccess.PathDiscovery; fiop.Assert(); try { fullPath = Path.GetFullPath(fileName); } finally { CodeAccessPermission.RevertAssert(); } new FileIOPermission(FileIOPermissionAccess.Read, fullPath).Demand(); if (!File.Exists(fullPath)) { throw new FileNotFoundException(fileName); } string companyName = ""; string fileDescription = ""; string fileVersion = ""; string internalName = ""; string legalCopyright = ""; string originalFilename = ""; string productName = ""; string productVersion = ""; string comments = ""; string legalTrademarks = ""; string privateBuild = ""; string specialBuild = ""; string language = ""; int fileMajor = 0; int fileMinor = 0; int fileBuild = 0; int filePrivate = 0; int productMajor = 0; int productMinor = 0; int productBuild = 0; int productPrivate = 0; int fileFlags = 0; int [] handle = new int[] { 0 }; int infoSize; infoSize = UnsafeNativeMethods.GetFileVersionInfoSize(fileName, handle); if (infoSize != 0) { IntPtr memHandle = UnsafeNativeMethods.GlobalAlloc(NativeMethods.GMEM_MOVEABLE | NativeMethods.GMEM_ZEROINIT, infoSize); try { IntPtr memPtr = UnsafeNativeMethods.GlobalLock(new HandleRef(null, memHandle)); try { if (UnsafeNativeMethods.GetFileVersionInfo(fileName, handle[0], infoSize, new HandleRef(null, memPtr))) { string codepage = ConvertTo8DigitHex(GetVarEntry(memPtr)); string template = "\\\\StringFileInfo\\\\{0}\\\\{1}"; companyName = GetFileVersionString(memPtr, string.Format(template, codepage, "CompanyName")); fileDescription = GetFileVersionString(memPtr, string.Format(template, codepage, "FileDescription")); fileVersion = GetFileVersionString(memPtr, string.Format(template, codepage, "FileVersion")); internalName = GetFileVersionString(memPtr, string.Format(template, codepage, "InternalName")); legalCopyright = GetFileVersionString(memPtr, string.Format(template, codepage, "LegalCopyright")); originalFilename = GetFileVersionString(memPtr, string.Format(template, codepage, "OriginalFilename")); productName = GetFileVersionString(memPtr, string.Format(template, codepage, "ProductName")); productVersion = GetFileVersionString(memPtr, string.Format(template, codepage, "ProductVersion")); comments = GetFileVersionString(memPtr, string.Format(template, codepage, "Comments")); legalTrademarks = GetFileVersionString(memPtr, string.Format(template, codepage, "LegalTrademarks")); privateBuild = GetFileVersionString(memPtr, string.Format(template, codepage, "PrivateBuild")); specialBuild = GetFileVersionString(memPtr, string.Format(template, codepage, "SpecialBuild")); language = GetFileVersionLanguage(memPtr); NativeMethods.VS_FIXEDFILEINFO ffi = GetFixedFileInfo(memPtr); fileMajor = HIWORD(ffi.dwFileVersionMS); fileMinor = LOWORD(ffi.dwFileVersionMS); fileBuild = HIWORD(ffi.dwFileVersionLS); filePrivate = LOWORD(ffi.dwFileVersionLS); productMajor = HIWORD(ffi.dwProductVersionMS); productMinor = LOWORD(ffi.dwProductVersionMS); productBuild = HIWORD(ffi.dwProductVersionLS); productPrivate = LOWORD(ffi.dwProductVersionLS); fileFlags = ffi.dwFileFlags; } } finally { UnsafeNativeMethods.GlobalUnlock(new HandleRef(null, memHandle)); } } finally { UnsafeNativeMethods.GlobalFree(new HandleRef(null, memHandle)); memHandle = IntPtr.Zero; } } return(new FileVersionInfo(fileName, companyName, fileDescription, fileVersion, internalName, legalCopyright, originalFilename, productName, productVersion, comments, legalTrademarks, privateBuild, specialBuild, language, fileMajor, fileMinor, fileBuild, filePrivate, productMajor, productMinor, productBuild, productPrivate, fileFlags)); }