CSharp08.Win32Wrap.GetFileVersionInfo C# (CSharp) Метод

GetFileVersionInfo() публичный статический Метод

public static GetFileVersionInfo ( string path ) : FileVersionInfo
path string
Результат FileVersionInfo
        public static FileVersionInfo GetFileVersionInfo(string path)
        {
            uint unuse = 0;
            uint bufSize = Win32Import.GetFileVersionInfoSize(path, ref unuse);
            if (bufSize != 0)
            {
                byte[] buf = new byte[bufSize];
                if (Win32Import.GetFileVersionInfo(path, 0, bufSize, buf) != 0)
                {
                    FileVersionInfo fileVersionInfo = new FileVersionInfo();

                    IntPtr data = IntPtr.Zero;
                    uint dataSize = 0;
                    if (Win32Import.VerQueryValue(buf, @"\", ref data, ref dataSize) > 0 &&
                        dataSize == Marshal.SizeOf(fileVersionInfo.fixedInfo))
                    {
                        fileVersionInfo.fixedInfo = (Win32Import.VS_FixedFileInfo)Marshal.PtrToStructure(
                            data, typeof(Win32Import.VS_FixedFileInfo));
                    }

                    short[] langCodeList = null;
                    if (Win32Import.VerQueryValue(buf, @"\VarFileInfo\Translation", ref data, ref dataSize) > 0)
                    {
                        langCodeList = new short[dataSize / 2];
                        Marshal.Copy(data, langCodeList, 0, langCodeList.Length);
                    }

                    if (langCodeList != null)
                    {
                        fileVersionInfo.langBasedInfoList = new FileVersionInfo.LanguageBasedFileInfo[langCodeList.Length / 2];
                        for (int i = 0; i < fileVersionInfo.langBasedInfoList.Length; ++i)
                        {
                            FileVersionInfo.LanguageBasedFileInfo langBasedInfo = new FileVersionInfo.LanguageBasedFileInfo();

                            langBasedInfo.language = unchecked((ushort)langCodeList[i * 2]);
                            langBasedInfo.codePage = unchecked((ushort)langCodeList[i * 2 + 1]);

                            Func<string, string> verQueryString = s =>
                            {
                                string fmt =
                                string.Format(
                                "\\StringFileInfo\\{0:x4}{1:x4}\\{2}",
                                langBasedInfo.language, langBasedInfo.codePage, s);

                                if (Win32Import.VerQueryValue(buf, fmt, ref data, ref dataSize) > 0)
                                {
                                    char[] tempBuf = new char[dataSize / 2];
                                    Marshal.Copy(data, tempBuf, 0, tempBuf.Length);
                                    return new string(tempBuf);
                                }
                                return string.Empty;
                            };

                            langBasedInfo.comments = verQueryString("Comments");
                            langBasedInfo.internalName = verQueryString("InternalName"); ;
                            langBasedInfo.productName = verQueryString("ProductName"); ;
                            langBasedInfo.companyName = verQueryString("CompanyName"); ;
                            langBasedInfo.legalCopyright = verQueryString("LegalCopyright"); ;
                            langBasedInfo.productVersion = verQueryString("ProductVersion"); ;
                            langBasedInfo.fileDescription = verQueryString("FileDescription"); ;
                            langBasedInfo.legalTrademarks = verQueryString("LegalTrademarks"); ;
                            langBasedInfo.privateBuild = verQueryString("PrivateBuild"); ;
                            langBasedInfo.fileVersion = verQueryString("FileVersion"); ;
                            langBasedInfo.originalFilename = verQueryString("OriginalFilename"); ;
                            langBasedInfo.specialBuild = verQueryString("SpecialBuild"); ;

                            fileVersionInfo.langBasedInfoList[i] = langBasedInfo;
                        }
                    }

                    return fileVersionInfo;
                }
            }

            return new FileVersionInfo();
        }

Usage Example

Пример #1
0
        static bool BuildUinstallItem(RegistryKey itemKey, ref UinstallItem item)
        {
            string IconPEPath = string.Empty;

            // 先准备最容易得到的数据
            item.RegistryKey = itemKey.Name.Substring(itemKey.Name.LastIndexOf('\\') + 1);

            item.DisplayName = GetStringValueFromRegistry(itemKey, "DisplayName");
            // item.QuietUninstall = itemKey.GetValue("QuietUninstallString", string.Empty).ToString();
            item.UninstallCmd  = GetStringValueFromRegistry(itemKey, "UninstallString");
            IconPEPath         = GetStringValueFromRegistry(itemKey, "DisplayIcon");
            item.InstallFolder = GetStringValueFromRegistry(itemKey, "InstallLocation");
            item.Version       = GetStringValueFromRegistry(itemKey, "DisplayVersion");
            item.CompanyName   = GetStringValueFromRegistry(itemKey, "Publisher");

            try
            {
                string s = GetStringValueFromRegistry(itemKey, "InstallDate");
                if (!string.IsNullOrEmpty(s))
                {
                    item.InstallDate = DateTime.ParseExact(s, "yyyyMMdd", null);
                }
            }
            catch { item.InstallDate = null; }

            // 得到重要的中间数据PEPath
            string PEPath = string.Empty;

            if (!string.IsNullOrEmpty(IconPEPath))
            {
                Match m = Regex.Match(IconPEPath, "\\p{L}:[^?<>|:\"]+?\\.exe");
                if (m.Success)
                {
                    PEPath = m.Groups[0].ToString();
                }
            }
            if (string.IsNullOrEmpty(PEPath) &&
                !string.IsNullOrEmpty(item.UninstallCmd))
            {
                Match m = Regex.Match(item.UninstallCmd, "\\p{L}:[^?<>|:\"]+?\\.exe");
                if (m.Success)
                {
                    PEPath = m.Groups[0].ToString();
                }
            }
            if (string.IsNullOrEmpty(PEPath) &&
                !string.IsNullOrEmpty(item.InstallFolder))
            {
                string[] files = Directory.GetFiles(item.InstallFolder, "*.exe");
                if (files != null && files.Length > 0)
                {
                    PEPath = files[0];
                }
            }

            // 根据PEPath反过来可以尝试修正一些数据
            if (!string.IsNullOrEmpty(PEPath))
            {
                if (string.IsNullOrEmpty(IconPEPath))
                {
                    IconPEPath = PEPath;
                }
                if (string.IsNullOrEmpty(item.InstallFolder))
                {
                    item.InstallFolder = Path.GetDirectoryName(PEPath);
                }
            }

            // 读取PEPath文件中的内容,修正一些数据
            if (!string.IsNullOrEmpty(PEPath))
            {
                Win32Wrap.FileVersionInfo info = Win32Wrap.GetFileVersionInfo(PEPath);
                if (info.langBasedInfoList != null && info.langBasedInfoList.Length > 0)
                {
                    Win32Wrap.FileVersionInfo.LanguageBasedFileInfo lanBasedInfo = info.langBasedInfoList[0];

                    item.Description = lanBasedInfo.fileDescription;
                    if (string.IsNullOrEmpty(item.DisplayName) &&
                        !string.IsNullOrEmpty(lanBasedInfo.productName))
                    {
                        item.DisplayName = lanBasedInfo.productName;
                    }
                    if (string.IsNullOrEmpty(item.CompanyName) &&
                        !string.IsNullOrEmpty(lanBasedInfo.companyName))
                    {
                        item.CompanyName = lanBasedInfo.companyName;
                    }
                    if (string.IsNullOrEmpty(item.Version) &&
                        !string.IsNullOrEmpty(lanBasedInfo.productVersion))
                    {
                        item.Version = lanBasedInfo.productVersion;
                    }
                }
            }

            if (!string.IsNullOrEmpty(IconPEPath))
            {
                int      iconIdx   = 0;
                IntPtr[] smallIcon = new IntPtr[1];

                {
                    Match m = Regex.Match(IconPEPath, "(.+),(\\d)$");
                    if (m.Success)
                    {
                        try
                        {
                            IconPEPath = m.Groups[1].ToString();
                            iconIdx    = int.Parse(m.Groups[2].ToString());
                        }
                        catch { }
                    }
                }

                if (IconPEPath.Length > 2 &&
                    IconPEPath.StartsWith("\"") &&
                    IconPEPath.EndsWith("\""))
                {
                    IconPEPath = IconPEPath.Substring(1, IconPEPath.Length - 2);
                }

                if (Win32Import.ExtractIconEx(IconPEPath, 0, null, smallIcon, 1) > 0 &&
                    smallIcon[0] != IntPtr.Zero)
                {
                    item.PEIcon = Icon.FromHandle(smallIcon[0]);
                }
            }
            if (item.PEIcon == null)
            {
                IntPtr[] smallIcon = new IntPtr[1];
                if (Win32Import.ExtractIconEx("shell32.dll", 2, null, smallIcon, 1) > 0 &&
                    smallIcon[0] != IntPtr.Zero)
                {
                    item.PEIcon = Icon.FromHandle(smallIcon[0]);
                }
            }

            // 修正安装日期
            if (item.InstallDate == null)
            {
                item.InstallDate = Win32Wrap.GetRegstryKeyLastWriteTime(
                    Win32Import.HKEY_LOCAL_MACHINE,
                    itemKey.Name.Substring(itemKey.Name.IndexOf('\\') + 1));
            }

            // 保证显示的数据存在
            if (string.IsNullOrEmpty(item.DisplayName) &&
                !string.IsNullOrEmpty(PEPath))
            {
                item.DisplayName = Path.GetFileNameWithoutExtension(PEPath);
            }

            return(!string.IsNullOrEmpty(item.DisplayName));
        }