实现效果:
知识运用:
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; } }
原文地址:https://www.cnblogs.com/feiyucha/p/10230797.html
时间: 2024-11-05 21:40:13