亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C# > 正文

c#根據文件類型獲取相關類型圖標的方法代碼

2020-01-24 03:23:47
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsFormsApplication1
{
    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using Microsoft.Win32;
    using System.Reflection;
    using System.Collections.Generic;

    namespace BlackFox.Win32
    {
        public static class Icons
        {
            #region Custom exceptions class

            public class IconNotFoundException : Exception
            {
                public IconNotFoundException(string fileName, int index)
                    : base(string.Format("Icon with Id = {0} wasn't found in file {1}", index, fileName))
                {
                }
            }

            public class UnableToExtractIconsException : Exception
            {
                public UnableToExtractIconsException(string fileName, int firstIconIndex, int iconCount)
                    : base(string.Format("Tryed to extract {2} icons starting from the one with id {1} from the /"{0}/" file but failed", fileName, firstIconIndex, iconCount))
                {
                }
            }

            #endregion

            #region DllImports

            /// <summary>
            /// Contains information about a file object.
            /// </summary>
            struct SHFILEINFO
            {
                /// <summary>
                /// Handle to the icon that represents the file. You are responsible for
                /// destroying this handle with DestroyIcon when you no longer need it.
                /// </summary>
                public IntPtr hIcon;

                /// <summary>
                /// Index of the icon image within the system image list.
                /// </summary>
                public IntPtr iIcon;

                /// <summary>
                /// Array of values that indicates the attributes of the file object.
                /// For information about these values, see the IShellFolder::GetAttributesOf
                /// method.
                /// </summary>
                public uint dwAttributes;

                /// <summary>
                /// String that contains the name of the file as it appears in the Microsoft
                /// Windows Shell, or the path and file name of the file that contains the
                /// icon representing the file.
                /// </summary>
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
                public string szDisplayName;

                /// <summary>
                /// String that describes the type of file.
                /// </summary>
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
                public string szTypeName;
            };

            [Flags]
            enum FileInfoFlags : int
            {
                /// <summary>
                /// Retrieve the handle to the icon that represents the file and the index
                /// of the icon within the system image list. The handle is copied to the
                /// hIcon member of the structure specified by psfi, and the index is copied
                /// to the iIcon member.
                /// </summary>
                SHGFI_ICON = 0x000000100,
                /// <summary>
                /// Indicates that the function should not attempt to access the file
                /// specified by pszPath. Rather, it should act as if the file specified by
                /// pszPath exists with the file attributes passed in dwFileAttributes.
                /// </summary>
                SHGFI_USEFILEATTRIBUTES = 0x000000010
            }

            /// <summary>
            ///     Creates an array of handles to large or small icons extracted from
            ///     the specified executable file, dynamic-link library (DLL), or icon
            ///     file.
            /// </summary>
            /// <param name="lpszFile">
            ///     Name of an executable file, DLL, or icon file from which icons will
            ///     be extracted.
            /// </param>
            /// <param name="nIconIndex">
            ///     <para>
            ///         Specifies the zero-based index of the first icon to extract. For
            ///         example, if this value is zero, the function extracts the first
            ///         icon in the specified file.
            ///     </para>
            ///     <para>
            ///         If this value is �1 and <paramref name="phiconLarge"/> and
            ///         <paramref name="phiconSmall"/> are both NULL, the function returns
            ///         the total number of icons in the specified file. If the file is an
            ///         executable file or DLL, the return value is the number of
            ///         RT_GROUP_ICON resources. If the file is an .ico file, the return
            ///         value is 1.
            ///     </para>
            ///     <para>
            ///         Windows 95/98/Me, Windows NT 4.0 and later: If this value is a
            ///         negative number and either <paramref name="phiconLarge"/> or
            ///         <paramref name="phiconSmall"/> is not NULL, the function begins by
            ///         extracting the icon whose resource identifier is equal to the
            ///         absolute value of <paramref name="nIconIndex"/>. For example, use -3
            ///         to extract the icon whose resource identifier is 3.
            ///     </para>
            /// </param>
            /// <param name="phIconLarge">
            ///     An array of icon handles that receives handles to the large icons
            ///     extracted from the file. If this parameter is NULL, no large icons
            ///     are extracted from the file.
            /// </param>
            /// <param name="phIconSmall">
            ///     An array of icon handles that receives handles to the small icons
            ///     extracted from the file. If this parameter is NULL, no small icons
            ///     are extracted from the file.
            /// </param>
            /// <param name="nIcons">
            ///     Specifies the number of icons to extract from the file.
            /// </param>
            /// <returns>
            ///     If the <paramref name="nIconIndex"/> parameter is -1, the
            ///     <paramref name="phIconLarge"/> parameter is NULL, and the
            ///     <paramref name="phiconSmall"/> parameter is NULL, then the return
            ///     value is the number of icons contained in the specified file.
            ///     Otherwise, the return value is the number of icons successfully
            ///     extracted from the file.
            /// </returns>
            [DllImport("Shell32", CharSet = CharSet.Auto)]
            extern static int ExtractIconEx(
                [MarshalAs(UnmanagedType.LPTStr)]
            string lpszFile,
                int nIconIndex,
                IntPtr[] phIconLarge,
                IntPtr[] phIconSmall,
                int nIcons);

            [DllImport("Shell32", CharSet = CharSet.Auto)]
            extern static IntPtr SHGetFileInfo(
                string pszPath,
                int dwFileAttributes,
                out SHFILEINFO psfi,
                int cbFileInfo,
                FileInfoFlags uFlags);

            #endregion

            /// <summary>
            /// Two constants extracted from the FileInfoFlags, the only that are
            /// meaningfull for the user of this class.
            /// </summary>
            public enum SystemIconSize : int
            {
                Large = 0x000000000,
                Small = 0x000000001
            }

            /// <summary>
            /// Get the number of icons in the specified file.
            /// </summary>
            /// <param name="fileName">Full path of the file to look for.</param>
            /// <returns></returns>
            static int GetIconsCountInFile(string fileName)
            {
                return ExtractIconEx(fileName, -1, null, null, 0);
            }

            #region ExtractIcon-like functions

            public static void ExtractEx(string fileName, List<Icon> largeIcons,
                List<Icon> smallIcons, int firstIconIndex, int iconCount)
            {
                /*
                 * Memory allocations
                 */

                IntPtr[] smallIconsPtrs = null;
                IntPtr[] largeIconsPtrs = null;

                if (smallIcons != null)
                {
                    smallIconsPtrs = new IntPtr[iconCount];
                }
                if (largeIcons != null)
                {
                    largeIconsPtrs = new IntPtr[iconCount];
                }

                /*
                 * Call to native Win32 API
                 */

                int apiResult = ExtractIconEx(fileName, firstIconIndex, largeIconsPtrs, smallIconsPtrs, iconCount);
                if (apiResult != iconCount)
                {
                    throw new UnableToExtractIconsException(fileName, firstIconIndex, iconCount);
                }

                /*
                 * Fill lists
                 */

                if (smallIcons != null)
                {
                    smallIcons.Clear();
                    foreach (IntPtr actualIconPtr in smallIconsPtrs)
                    {
                        smallIcons.Add(Icon.FromHandle(actualIconPtr));
                    }
                }
                if (largeIcons != null)
                {
                    largeIcons.Clear();
                    foreach (IntPtr actualIconPtr in largeIconsPtrs)
                    {
                        largeIcons.Add(Icon.FromHandle(actualIconPtr));
                    }
                }
            }

            public static List<Icon> ExtractEx(string fileName, SystemIconSize size,
                int firstIconIndex, int iconCount)
            {
                List<Icon> iconList = new List<Icon>();

                switch (size)
                {
                    case SystemIconSize.Large:
                        ExtractEx(fileName, iconList, null, firstIconIndex, iconCount);
                        break;

                    case SystemIconSize.Small:
                        ExtractEx(fileName, null, iconList, firstIconIndex, iconCount);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("size");
                }

                return iconList;
            }

            public static void Extract(string fileName, List<Icon> largeIcons, List<Icon> smallIcons)
            {
                int iconCount = GetIconsCountInFile(fileName);
                ExtractEx(fileName, largeIcons, smallIcons, 0, iconCount);
            }

            public static List<Icon> Extract(string fileName, SystemIconSize size)
            {
                int iconCount = GetIconsCountInFile(fileName);
                return ExtractEx(fileName, size, 0, iconCount);
            }

            public static Icon ExtractOne(string fileName, int index, SystemIconSize size)
            {
                try
                {
                    List<Icon> iconList = ExtractEx(fileName, size, index, 1);
                    return iconList[0];
                }
                catch (UnableToExtractIconsException)
                {
                    throw new IconNotFoundException(fileName, index);
                }
            }

            public static void ExtractOne(string fileName, int index,
                out Icon largeIcon, out Icon smallIcon)
            {
                List<Icon> smallIconList = new List<Icon>();
                List<Icon> largeIconList = new List<Icon>();
                try
                {
                    ExtractEx(fileName, largeIconList, smallIconList, index, 1);
                    largeIcon = largeIconList[0];
                    smallIcon = smallIconList[0];
                }
                catch (UnableToExtractIconsException)
                {
                    throw new IconNotFoundException(fileName, index);
                }
            }

            #endregion

            //this will look throw the registry
            //to find if the Extension have an icon.
            public static Icon IconFromExtension(string extension,
                                                    SystemIconSize size)
            {
                // Add the '.' to the extension if needed
                if (extension[0] != '.') extension = '.' + extension;

                //opens the registry for the wanted key.
                RegistryKey Root = Registry.ClassesRoot;
                RegistryKey ExtensionKey = Root.OpenSubKey(extension);
                ExtensionKey.GetValueNames();
                RegistryKey ApplicationKey =
                    Root.OpenSubKey(ExtensionKey.GetValue("").ToString());

                //gets the name of the file that have the icon.
                string IconLocation =
                    ApplicationKey.OpenSubKey("DefaultIcon").GetValue("").ToString();
                string[] IconPath = IconLocation.Split(',');

                if (IconPath[1] == null) IconPath[1] = "0";
                IntPtr[] Large = new IntPtr[1], Small = new IntPtr[1];

                //extracts the icon from the file.
                ExtractIconEx(IconPath[0],
                    Convert.ToInt16(IconPath[1]), Large, Small, 1);
                return size == SystemIconSize.Large ?
                    Icon.FromHandle(Large[0]) : Icon.FromHandle(Small[0]);
            }

            public static Icon IconFromExtensionShell(string extension, SystemIconSize size)
            {
                //add '.' if nessesry
                if (extension[0] != '.') extension = '.' + extension;

                //temp struct for getting file shell info
                SHFILEINFO fileInfo = new SHFILEINFO();

                SHGetFileInfo(
                    extension,
,
                    out fileInfo,
                    Marshal.SizeOf(fileInfo),
                    FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_USEFILEATTRIBUTES | (FileInfoFlags)size);

                return Icon.FromHandle(fileInfo.hIcon);
            }

            public static Icon IconFromResource(string resourceName)
            {
                Assembly assembly = Assembly.GetCallingAssembly();

                return new Icon(assembly.GetManifestResourceStream(resourceName));
            }

            /// <summary>
            /// Parse strings in registry who contains the name of the icon and
            /// the index of the icon an return both parts.
            /// </summary>
            /// <param name="regString">The full string in the form "path,index" as found in registry.</param>
            /// <param name="fileName">The "path" part of the string.</param>
            /// <param name="index">The "index" part of the string.</param>
            public static void ExtractInformationsFromRegistryString(
                string regString, out string fileName, out int index)
            {
                if (regString == null)
                {
                    throw new ArgumentNullException("regString");
                }
                if (regString.Length == 0)
                {
                    throw new ArgumentException("The string should not be empty.", "regString");
                }

                index = 0;
                string[] strArr = regString.Replace("/"", "").Split(',');
                fileName = strArr[0].Trim();
                if (strArr.Length > 1)
                {
                    int.TryParse(strArr[1].Trim(), out index);
                }
            }

            public static Icon ExtractFromRegistryString(string regString, SystemIconSize size)
            {
                string fileName;
                int index;
                ExtractInformationsFromRegistryString(regString, out fileName, out index);
                return ExtractOne(fileName, index, size);
            }
        }
    }
}       

復制代碼 代碼如下:

/// <summary>
        /// 應用程序的主入口點。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            PictureBox pict = new PictureBox();
            pict.Image = BlackFox.Win32.Icons.IconFromExtension(".zip", BlackFox.Win32.Icons.SystemIconSize.Large).ToBitmap();
            pict.Dock = DockStyle.Fill;
            pict.SizeMode = PictureBoxSizeMode.CenterImage;

            Form form = new Form();
            form.Controls.Add(pict);
            Application.Run(form);
        }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
久久久精品久久久| 91精品久久久久久久久青青| 国产精品黄色影片导航在线观看| 国产日韩欧美黄色| 日本高清久久天堂| 黑人巨大精品欧美一区二区免费| 久久天天躁狠狠躁夜夜躁2014| 精品亚洲一区二区三区在线观看| 色哟哟入口国产精品| 高清一区二区三区日本久| 久久九九有精品国产23| 在线观看国产精品91| 日韩电视剧在线观看免费网站| 亚洲电影第1页| 国产成人高潮免费观看精品| 日韩在线免费av| 亚洲精品美女在线观看播放| 一区二区三欧美| 亚洲成av人片在线观看香蕉| 国产精品亚洲一区二区三区| 国产精品一区二区久久久久| 欧美男插女视频| 国产精品爽爽ⅴa在线观看| 亚洲aⅴ男人的天堂在线观看| 国产中文字幕亚洲| 亚洲一区二区三区在线视频| 亚洲精品久久7777777| 日韩欧美综合在线视频| 精品国产欧美一区二区五十路| 色婷婷综合久久久久中文字幕1| 欧美日韩成人在线播放| 日韩av一区二区在线观看| 亚洲japanese制服美女| 亚洲一区亚洲二区亚洲三区| 久久天天躁狠狠躁夜夜躁| 最近2019中文免费高清视频观看www99| 久久在线精品视频| 亚洲成人av在线播放| 欧美性高跟鞋xxxxhd| 国内精品久久久久久影视8| 国产成人精品久久二区二区91| 北条麻妃99精品青青久久| 亚洲精品一区二区三区不| 亚洲欧美日韩中文在线制服| 综合激情国产一区| 69视频在线免费观看| 国产日韩欧美影视| 45www国产精品网站| 91久久久国产精品| 精品美女永久免费视频| 久久精品国产v日韩v亚洲| 美女精品视频一区| 日韩精品在线免费播放| 中文字幕欧美亚洲| 欧美午夜片欧美片在线观看| 最近2019中文字幕在线高清| 国产精品美腿一区在线看| 欧美午夜美女看片| 国产日本欧美一区二区三区在线| 97精品国产91久久久久久| 久久伊人精品一区二区三区| 国语对白做受69| 久久久女人电视剧免费播放下载| 欧美成人三级视频网站| 欧美理论电影网| 日本午夜在线亚洲.国产| 国产一级揄自揄精品视频| 蜜月aⅴ免费一区二区三区| 日韩欧美在线观看视频| 亚洲图片欧洲图片av| 亚洲一区制服诱惑| 欧美精品情趣视频| 欧美另类精品xxxx孕妇| 亚洲精品白浆高清久久久久久| 亚洲精品久久久久久久久久久久久| 久久亚洲精品小早川怜子66| 亚洲美女视频网| 国产精品影片在线观看| 欧美色图在线视频| 97国产在线视频| 日韩欧美在线字幕| 日韩成人在线视频观看| 欧美午夜宅男影院在线观看| 日本成人精品在线| 久久精品视频播放| 国产精品av网站| 国产婷婷色综合av蜜臀av| 国模叶桐国产精品一区| 成人中心免费视频| 亚洲第一精品自拍| 国产成人福利视频| 国产精品99久久久久久久久| 中文字幕精品在线视频| 成人免费自拍视频| 久久国产精品影片| 欧美精品videossex88| 国产在线精品成人一区二区三区| 国产伦精品一区二区三区精品视频| 国自产精品手机在线观看视频| 免费97视频在线精品国自产拍| 国产精品久久久久久av下载红粉| 欧美性感美女h网站在线观看免费| 欧美午夜丰满在线18影院| 日本人成精品视频在线| 久久91亚洲人成电影网站| 久久天天躁狠狠躁夜夜爽蜜月| 久久伊人精品天天| 国产成人激情小视频| 搡老女人一区二区三区视频tv| 91久热免费在线视频| 国产精品久久久久久久久久新婚| 中文字幕亚洲欧美一区二区三区| 亚洲bt欧美bt日本bt| 在线色欧美三级视频| 国产啪精品视频| 91高清视频在线免费观看| 成人欧美一区二区三区黑人孕妇| 日韩风俗一区 二区| 欧美xxxx14xxxxx性爽| 亚洲人成在线观| 欧美黄色片免费观看| 国产一区二区三区网站| 欲色天天网综合久久| 欧美性理论片在线观看片免费| zzijzzij亚洲日本成熟少妇| 国产成人一区二区在线| 一区二区三区四区在线观看视频| 日韩美女在线播放| 亚洲精品在线看| 国产精品中文久久久久久久| 久久视频国产精品免费视频在线| 亚洲欧美视频在线| 亚洲精品不卡在线| 亚洲国产精久久久久久| 国产精品三级网站| 国产精品福利网| 欧美一级在线播放| 亚洲伦理中文字幕| 久久99久国产精品黄毛片入口| 亚洲国产成人在线视频| 国产一区二区在线免费视频| 日韩中文字在线| 精品自在线视频| 午夜精品在线视频| 欧日韩在线观看| 欧美国产日韩xxxxx| 日日狠狠久久偷偷四色综合免费| 国产精品羞羞答答| 日韩精品高清视频| 精品成人在线视频| 精品福利在线观看| 亚洲第一级黄色片| 国产欧美精品一区二区三区介绍| 欧美中文在线观看国产| 国产精品第3页| 亚洲福利视频久久| 日韩精品小视频| 久久av资源网站| 久久夜精品va视频免费观看| 精品成人乱色一区二区| 国产精品免费观看在线| 深夜成人在线观看| 久久久国产精彩视频美女艺术照福利| 九九热精品视频国产|