获取文件夹中的图标资源
资源 获取 文件夹 图标
2023-09-14 09:00:20 时间
实现效果:
知识运用:
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; } }
相关文章
- 提升资源利用率与保障服务质量,鱼与熊掌不可兼得?
- RP原型资源分享-购物类App
- Silverlight游戏设计(Game Design):(十四)练习用游戏素材资源的获取及相关工具使用心得…[通俗易懂]
- 实验十(课程资源)-DNS服务器配置与管理[通俗易懂]
- 多线程资源并发
- J2ME开发站点资源「建议收藏」
- 如何避免用户通过浏览器控制台查看页面资源
- 找资源什么的,这样搜索才高效
- 免费模型资源——精灵之刃
- 干货分享-Prelude的安装方法+如何获取资源
- 疾病研究资源 | 用于评估疾病相关性的生物医学数据、计算方法和工具
- 【Android 进程保活】应用进程拉活 ( JobScheduler 拉活 | JobScheduler 使用流程 | JobService 服务 | 不同版本兼容 | 源码资源 )
- 【Android 插件化】“ 插桩式 “ 插件化框架 ( 获取插件入口 Activity 组件 | 加载插件 Resources 资源 )
- 【每日随笔】使用 you-get 获取网页中的视频资源 ( Python 环境安装 | you-get 工具使用 )
- AI下载AI最新版软件介绍---Illustrator干货分享++资源获取
- 一文了解 TKG 如何使用 GPU 资源池
- [ChatGPT]-02-ChatGPT对安全的影响和开源的LLM大模型资源汇总
- java获取资源文件详解编程语言
- 从 Linux 源码网站获取开源资源(linux源码网站)
- 资源免费享受Oracle软件资源(oracle免费)
- Linux 神器下载秘籍,轻松解决资源获取难题!(linux如何下载东西)
- 资源从Redis获取资源:一次有效的缓存读取(从redis 获取)
- Oracle官网登录:轻松获取最新技术信息和产品资源(oracle官网登录)
- 挫败Redis获取资源的企图(获取redis资源失败)
- MYSQL 电视剧片段免费下载指南,轻松获取完整影视资源(MYSQL下载电视剧片段)
- Redis连接池满载防止资源耗尽(redis连接池被占用)
- 基于Redis实现的分布式资源锁(redis 资源锁)
- 简单易用的Redis资源监控工具,让你第一时间看到 redis状态(redis资源监控工具)
- PHP+MYSQL开发工具及资源收藏
- asp.net中资源文件的使用