//获取指定路径下所有文件及其图标 public void GetListViewItem(string path, ImageList imglist, ListView lv) { lv.Items.Clear(); Win32.SHFILEINFO shfi = new Win32.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]); if (dir.Name == "RECYCLER" || dir.Name == "RECYCLED" || dir.Name == "Recycled" || dir.Name == "System Volume Information") { } else { //获得图标 Win32.SHGetFileInfo(dirs[i], (uint)0x80, ref shfi, (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi), (uint)(0x100 | 0x400)); //取得Icon和TypeName //添加图标 imglist.Images.Add(dir.Name, (Icon)Icon.FromHandle(shfi.hIcon).Clone()); info[0] = dir.Name; info[1] = ""; info[2] = "文件夹"; info[3] = dir.LastWriteTime.ToString(); ListViewItem item = new ListViewItem(info, dir.Name); lv.Items.Add(item); //销毁图标 Win32.DestroyIcon(shfi.hIcon); } } for (int i = 0; i < files.Length; i++) { string[] info = new string[4]; FileInfo fi = new FileInfo(files[i]); string Filetype = fi.Name.Substring(fi.Name.LastIndexOf(".") + 1, fi.Name.Length - fi.Name.LastIndexOf(".") - 1); string newtype = Filetype.ToLower(); if (newtype == "sys" || newtype == "ini" || newtype == "bin" || newtype == "log" || newtype == "com" || newtype == "bat" || newtype == "db") { } else { //获得图标 Win32.SHGetFileInfo(files[i], (uint)0x80, ref shfi, (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi), (uint)(0x100 | 0x400)); //取得Icon和TypeName //添加图标 imglist.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.LastWriteTime.ToString(); ListViewItem item = new ListViewItem(info, fi.Name); lv.Items.Add(item); //销毁图标 Win32.DestroyIcon(shfi.hIcon); } } } catch { } }
/// 给出文件扩展名(.*),返回相应图标 /// 若不以"."开头则返回文件夹的图标。 public 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 { fileIcon = new string[] { systemDirectory + "shell32.dll", "2" }; //调用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); } return(resultIcon); }