Dictionary <string, Image> _dicSystemFileLaregeIcon = new Dictionary <string, Image>(); //大图标 ///<summary> ///根据文件路径获取其系统图标. (支持两种) ///</summary> KeyValuePair <string, Image> GetImageAndKey(string path, GetSystemIconType iconType) { if (iconType == GetSystemIconType.ExtensionSmall) { return(GetImageAndKey(path, _dicSystemFileSmallIcon, iconType)); } else if (iconType == GetSystemIconType.ExtensionLarge) { return(GetImageAndKey(path, _dicSystemFileLaregeIcon, iconType)); } return(new KeyValuePair <string, Image>(null, null)); }
/// <summary> /// 获取此文件(或扩展名)的系统图标 /// </summary> public static System.Drawing.Icon GetSystemIcon(string filePath, GetSystemIconType type) { System.Drawing.Icon tempIcon = null; try { switch (type) { case GetSystemIconType.ExtensionSmall: if (filePath[0] != '.') { tempIcon = GetSmallIcon(System.IO.Path.GetExtension(filePath)); } else { tempIcon = GetSmallIcon(System.IO.Path.GetFullPath(filePath)); } break; case GetSystemIconType.ExtensionLarge: if (filePath[0] != '.') { tempIcon = GetLargeIcon(System.IO.Path.GetExtension(filePath)); } else { tempIcon = GetLargeIcon(System.IO.Path.GetFullPath(filePath)); } break; case GetSystemIconType.PathSmall: tempIcon = GetSmallIconForPath(System.IO.Path.GetFullPath(filePath)); break; case GetSystemIconType.PathLarge: tempIcon = GetLargeIconForPath(System.IO.Path.GetFullPath(filePath)); break; default: throw new ArgumentException("未知的参数:" + type); } } catch { #if DEBUG throw; #endif } return(tempIcon); }
KeyValuePair <string, Image> GetImageAndKey(string path, Dictionary <string, Image> fileIcon, GetSystemIconType iconType) { string extension = Path.GetExtension(path); //无扩展名时特殊处理 if ((!Utility.File.IsDirectory(path)) && extension == "") { string strSign = "?noextension"; if (fileIcon.ContainsKey(strSign)) { return(new KeyValuePair <string, Image>(strSign, fileIcon[strSign])); } else { Icon ico = Service.Icon.GetSystemIcon(strSign, iconType); if (ico != null) { Image img = ico.ToBitmap(); fileIcon.Add(strSign, img); return(new KeyValuePair <string, Image>(strSign, img)); } else { return(new KeyValuePair <string, Image>(null, null)); } } } ///如果是文件夹,.exe文件,.lnk文件,则找其具体的图标 else if (Utility.File.IsDirectory(path) || extension == ".exe" || extension == ".lnk") { if (iconType == GetSystemIconType.ExtensionLarge) { iconType = GetSystemIconType.PathLarge; } else if (iconType == GetSystemIconType.ExtensionSmall) { iconType = GetSystemIconType.PathSmall; } Icon ico = Service.Icon.GetSystemIcon(path, iconType); if (ico != null) { Image img = ico.ToBitmap(); if (!fileIcon.ContainsKey(path)) { fileIcon.Add(path, img); } return(new KeyValuePair <string, Image>(path, img)); } else { return(new KeyValuePair <string, Image>(null, null)); } } ///其它的找扩展名的图标 else { if (fileIcon.ContainsKey(extension)) { return(new KeyValuePair <string, Image>(extension, fileIcon[extension])); } else { Icon ico = Service.Icon.GetSystemIcon(extension, iconType); if (ico != null) { Image img = ico.ToBitmap(); fileIcon.Add(extension, img); return(new KeyValuePair <string, Image>(extension, img)); } else { return(new KeyValuePair <string, Image>(null, null)); } } } }