System.Diagnostics.FileVersionInfo.GetVersionInfoForCodePage C# (CSharp) Method

GetVersionInfoForCodePage() private method

private GetVersionInfoForCodePage ( IntPtr memIntPtr, string codepage ) : bool
memIntPtr IntPtr
codepage string
return bool
        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);

            Interop.Version.VS_FIXEDFILEINFO ffi = GetFixedFileInfo(memIntPtr);
            _fileMajor = (int)HIWORD(ffi.dwFileVersionMS);
            _fileMinor = (int)LOWORD(ffi.dwFileVersionMS);
            _fileBuild = (int)HIWORD(ffi.dwFileVersionLS);
            _filePrivate = (int)LOWORD(ffi.dwFileVersionLS);
            _productMajor = (int)HIWORD(ffi.dwProductVersionMS);
            _productMinor = (int)LOWORD(ffi.dwProductVersionMS);
            _productBuild = (int)HIWORD(ffi.dwProductVersionLS);
            _productPrivate = (int)LOWORD(ffi.dwProductVersionLS);

            _isDebug = (ffi.dwFileFlags & (uint)Interop.Version.FileVersionInfo.VS_FF_DEBUG) != 0;
            _isPatched = (ffi.dwFileFlags & (uint)Interop.Version.FileVersionInfo.VS_FF_PATCHED) != 0;
            _isPrivateBuild = (ffi.dwFileFlags & (uint)Interop.Version.FileVersionInfo.VS_FF_PRIVATEBUILD) != 0;
            _isPreRelease = (ffi.dwFileFlags & (uint)Interop.Version.FileVersionInfo.VS_FF_PRERELEASE) != 0;
            _isSpecialBuild = (ffi.dwFileFlags & (uint)Interop.Version.FileVersionInfo.VS_FF_SPECIALBUILD) != 0;

            // fileVersion is chosen based on best guess. Other fields can be used if appropriate.
            return (_fileVersion != string.Empty);
        }

Usage Example

Esempio n. 1
0
        /// <summary>
        /// Returns a System.Windows.Forms.FileVersionInfo representing the version information associated with the specified file.
        /// </summary>
        public unsafe static FileVersionInfo GetVersionInfo(string fileName)
        {
            // Check for the existence of the file. File.Exists returns false
            // if Read permission is denied.
            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException(fileName);
            }

            uint handle;  // This variable is not used, but we need an out variable.
            uint infoSize = Interop.mincore.GetFileVersionInfoSizeEx(
                (uint)Interop.Constants.FileVerGetLocalised, fileName, out handle);
            FileVersionInfo versionInfo = new FileVersionInfo(fileName);

            if (infoSize != 0)
            {
                byte[] mem = new byte[infoSize];
                fixed(byte *memPtr = mem)
                {
                    IntPtr memIntPtr = new IntPtr((void *)memPtr);

                    if (Interop.mincore.GetFileVersionInfoEx(
                            (uint)Interop.Constants.FileVerGetLocalised | (uint)Interop.Constants.FileVerGetNeutral,
                            fileName,
                            0U,
                            infoSize,
                            memIntPtr))
                    {
                        uint langid = GetVarEntry(memIntPtr);
                        if (!versionInfo.GetVersionInfoForCodePage(memIntPtr, ConvertTo8DigitHex(langid)))
                        {
                            // Some dlls might not contain correct codepage information. In this case we will fail during lookup.
                            // Explorer will take a few shots in dark by trying following ID:
                            //
                            // 040904B0 // US English + CP_UNICODE
                            // 040904E4 // US English + CP_USASCII
                            // 04090000 // US English + unknown codepage
                            // Explorer also randomly guess 041D04B0=Swedish+CP_UNICODE and 040704B0=German+CP_UNICODE) sometimes.
                            // We will try to simulate similiar behavior here.
                            uint[] ids = new uint[] { 0x040904B0, 0x040904E4, 0x04090000 };
                            foreach (uint id in ids)
                            {
                                if (id != langid)
                                {
                                    if (versionInfo.GetVersionInfoForCodePage(memIntPtr, ConvertTo8DigitHex(id)))
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(versionInfo);
        }
All Usage Examples Of System.Diagnostics.FileVersionInfo::GetVersionInfoForCodePage