码迷,mamicode.com
首页 > 其他好文 > 详细

获取文件夹中的图标资源

时间:2019-01-06 23:03:15      阅读:330      评论:0      收藏:0      [点我收藏+]

标签:seq   click   指定   gre   cte   pre   imp   ges   []   

实现效果:

  技术分享图片

知识运用:

  API函数SHGetFileInfo    //获取包含在可执行文件或Dll中的图标数或图标资源

  [DllImport("shell32.dll", EntryPoint = "SHGetFileInfo")]
  public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttribute, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint Flags);

  技术分享图片

  和ExtractIconEx函数      //从限定的可执行文件 动态链接库 或者图标文件中生成图标句柄数组

  [DllImport("shell32.dll")]
  public static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons);

  技术分享图片

实现代码:

        [DllImport("shell32.dll", EntryPoint = "SHGetFileInfo")]
        public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttribute, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint Flags);
        [DllImport("User32.dll", EntryPoint = "DestroyIcon")]
        public static extern int DestroyIcon(IntPtr hIcon);
        [DllImport("shell32.dll")]
        public static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons);
        [StructLayout(LayoutKind.Sequential)]
        public struct SHFILEINFO
        {
            public IntPtr hIcon;
            public IntPtr iIcon;
            public uint dwAttributes;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szDisplayName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
            public string szTypeName;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 0)
                GetlistViewItem(textBox1.Text,imageList1,lv1);
        }

        public void GetlistViewItem(string path, ImageList imagelist, ListView lv)      //获取指定路径下的所有文件及其图标
        {
            lv.Items.Clear();
            SHFILEINFO shfi = new SHFILEINFO();                                         //创建SHFILEINFO对象
            try
            {
                string[] dirs = Directory.GetDirectories(path);                         //获取指定目录中子目录的名称
                string[] files = Directory.GetFiles(path);                              //获取指定路径中文件的名称
                for (int i = 0; i < dirs.Length; i++)                                   //遍历子文件夹
                {
                    string[] info = new string[4];                                      //定义一个数组
                    DirectoryInfo dir = new DirectoryInfo(dirs[i]);                     //根据文件夹路径创建DirectoryInfo对象
                    if (!(dir.Name == "RECYCLER" || dir.Name == "RECYCLED" || dir.Name == "Recycled" || dir.Name == "System Volume Infomation"))
                    {
                        //获取文件夹的图标及类型
                        SHGetFileInfo(dirs[i], (uint)0x80, ref shfi, (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi), (uint)(0x100 | 0x400));
                        imagelist.Images.Add(dir.Name, (Icon)Icon.FromHandle(shfi.hIcon).Clone());  //添加图标
                        info[0] = dir.Name;                                             //获取文件夹名称
                        info[1] = "";                                                   //获取文件夹大小
                        info[2] = "文件夹";                                             //获取类型
                        info[3] = dir.LastAccessTime.ToString();                        //获取修改时间
                        ListViewItem item = new ListViewItem(info,dir.Name);            //创建ListViewItem对象
                        lv.Items.Add(item);                                             //添加当前文件夹的基本信息
                        DestroyIcon(shfi.hIcon);                                        //销毁图标
                    }

                }
                for (int i = 0; i < files.Length; i++)                                  //遍历目录下的文件
                {
                    string[] info = new string[4];
                    FileInfo fi=new FileInfo(files[i]);
                    string Filetype=files[i].Substring(files[i].LastIndexOf(".")+1,files[i].Length-files[i].LastIndexOf(".")-1);
                    string Newtype=Filetype.ToString();
                    if (!(Newtype == "sys" || Newtype == "ini" || Newtype == "bin" || Newtype ==  "log" || Newtype == "com" || Newtype == "bat" || Newtype == "db"))
                    {
                        SHGetFileInfo(files[i], (uint)0x80, ref shfi, (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi), (uint)(0x100 | 0x400));
                        imagelist.Images.Add(fi.Name, (Icon)Icon.FromHandle(shfi.hIcon).Clone());
                        info[0] = fi.Name;
                        info[1] = fi.Length.ToString();
                        info[2] = fi.Extension.ToString();
                        info[3] = fi.LastAccessTime.ToString();
                        ListViewItem item = new ListViewItem(info, fi.Name);
                        lv.Items.Add(item);
                        DestroyIcon(shfi.hIcon);
                    }
                }
            }
            catch{}
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;
            }
        }

  

获取文件夹中的图标资源

标签:seq   click   指定   gre   cte   pre   imp   ges   []   

原文地址:https://www.cnblogs.com/feiyucha/p/10230797.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!