C#中读取exe文件的icon类

<strong><span style="font-size:18px;"> public class GetSystemIcon
    {
        /// <summary>

        /// 依据文件名读取图标,若指定文件不存在,则返回空值。  

        /// </summary>

        /// <param name="fileName">文件路径</param>

        /// <param name="isLarge">是否返回大图标</param>

        /// <returns></returns>

        public static Icon GetIconByFileName(string fileName, bool isLarge = true)
        {

            int[] phiconLarge = new int[1];

            int[] phiconSmall = new int[1];

            //文件名 图标索引 

            Win32.ExtractIconEx(fileName, 0, phiconLarge, phiconSmall, 1);

            IntPtr IconHnd = new IntPtr(isLarge ? phiconLarge[0] : phiconSmall[0]);

            return Icon.FromHandle(IconHnd);

        }
        /// <summary>  

        /// 根据文件扩展名(如:.*),返回与之关联的图标。

        /// 若不以"."开头则返回文件夹的图标。  

        /// </summary>  

        /// <param name="fileType">文件扩展名</param>  

        /// <param name="isLarge">是否返回大图标</param>  

        /// <returns></returns>  

        public static Icon GetIconByFileType(string fileType, bool isLarge)
        {

            if (fileType == null || fileType.Equals(string.Empty)) return null;



            RegistryKey regVersion = null;

            string regFileType = null;

            string regIconString = null;

            string systemDirectory = Environment.SystemDirectory + "\\";



            if (fileType[0] == '.')
            {

                //读系统注册表中文件类型信息  

                regVersion = Registry.ClassesRoot.OpenSubKey(fileType, true);

                if (regVersion != null)
                {

                    regFileType = regVersion.GetValue("") as string;

                    regVersion.Close();

                    regVersion = Registry.ClassesRoot.OpenSubKey(regFileType + @"\DefaultIcon", true);

                    if (regVersion != null)
                    {

                        regIconString = regVersion.GetValue("") as string;

                        regVersion.Close();

                    }

                }

                if (regIconString == null)
                {

                    //没有读取到文件类型注册信息,指定为未知文件类型的图标  

                    regIconString = systemDirectory + "shell32.dll,0";

                }

            }

            else
            {

                //直接指定为文件夹图标  

                regIconString = systemDirectory + "shell32.dll,3";

            }

            string[] fileIcon = regIconString.Split(new char[] { ',' });

            if (fileIcon.Length != 2)
            {

                //系统注册表中注册的标图不能直接提取,则返回可执行文件的通用图标  

                fileIcon = new string[] { systemDirectory + "shell32.dll", "2" };

            }

            Icon resultIcon = null;

            try
            {

                //调用API方法读取图标  

                int[] phiconLarge = new int[1];

                int[] phiconSmall = new int[1];

                uint count = Win32.ExtractIconEx(fileIcon[0], Int32.Parse(fileIcon[1]), phiconLarge, phiconSmall, 1);

                IntPtr IconHnd = new IntPtr(isLarge ? phiconLarge[0] : phiconSmall[0]);

                resultIcon = Icon.FromHandle(IconHnd);

            }

            catch { }

            return resultIcon;

        }
    }

    /// <summary>  

    /// 定义调用的API方法  

    /// </summary>  

    class Win32
    {

        [DllImport("shell32.dll")]

        public static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons);



    }</span></strong>
<strong><span style="font-size:18px;">要注意引用下面这些</span></strong>
<strong><span style="font-size:18px;">using System.Drawing;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.IO;</span></strong>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值