说明:本例将目录中的文件显示在窗体的ListView控件中,并定义了多种视图浏览。通过调用Win32库函数实现图标数据的提取。

主程序:

大图标:

列表:

详细信息:

Form1.cs:

public partial class Form1 : Form{FileInfoList fileList;public Form1(){InitializeComponent();}private void 加载文件ToolStripMenuItem_Click(object sender, EventArgs e){FolderBrowserDialog dlg = new FolderBrowserDialog();if (dlg.ShowDialog() == DialogResult.OK){string[] filespath = Directory.GetFiles(dlg.SelectedPath);fileList = new FileInfoList(filespath);InitListView();}}private void InitListView(){listView1.Items.Clear();this.listView1.BeginUpdate();foreach (FileInfoWithIcon file in fileList.list){ListViewItem item = new ListViewItem();item.Text = file.fileInfo.Name.Split('.')[0];item.ImageIndex = file.iconIndex;item.SubItems.Add(file.fileInfo.LastWriteTime.ToString());item.SubItems.Add(file.fileInfo.Extension.Replace(".",""));item.SubItems.Add(string.Format(("{0:N0}"), file.fileInfo.Length));listView1.Items.Add(item);}listView1.LargeImageList = fileList.imageListLargeIcon;listView1.SmallImageList = fileList.imageListSmallIcon;listView1.Show();this.listView1.EndUpdate();}private void 大图标ToolStripMenuItem_Click(object sender, EventArgs e){listView1.View = View.LargeIcon;}private void 小图标ToolStripMenuItem_Click(object sender, EventArgs e){listView1.View = View.SmallIcon;}private void 平铺ToolStripMenuItem_Click(object sender, EventArgs e){listView1.View = View.Tile;}private void 列表ToolStripMenuItem_Click(object sender, EventArgs e){listView1.View = View.List;}private void 详细信息ToolStripMenuItem_Click(object sender, EventArgs e){listView1.View = View.Details;}}

FileInfoList.cs:

说明:主要用于后台数据的存储
class FileInfoList{public List<FileInfoWithIcon> list;public ImageList imageListLargeIcon;public ImageList imageListSmallIcon;/// <summary>/// 根据文件路径获取生成文件信息,并提取文件的图标/// </summary>/// <param name="filespath"></param>public FileInfoList(string[] filespath){list = new List<FileInfoWithIcon>();imageListLargeIcon = new ImageList();imageListLargeIcon.ImageSize = new Size(32, 32);imageListSmallIcon = new ImageList();imageListSmallIcon.ImageSize = new Size(16, 16);foreach (string path in filespath){FileInfoWithIcon file = new FileInfoWithIcon(path);imageListLargeIcon.Images.Add(file.largeIcon);imageListSmallIcon.Images.Add(file.smallIcon);file.iconIndex = imageListLargeIcon.Images.Count - 1;list.Add(file);}}}class FileInfoWithIcon{public FileInfo fileInfo;public Icon largeIcon;public Icon smallIcon;public int iconIndex;public FileInfoWithIcon(string path){fileInfo = new FileInfo(path);largeIcon = GetSystemIcon.GetIconByFileName(path, true);if (largeIcon == null)largeIcon = GetSystemIcon.GetIconByFileType(Path.GetExtension(path), true);smallIcon = GetSystemIcon.GetIconByFileName(path, false);if (smallIcon == null)smallIcon = GetSystemIcon.GetIconByFileType(Path.GetExtension(path), false);}}

GetSystemIcon:

说明:定义两种图标获取方式,从文件提取和从文件关联的系统资源中提取。
public static class GetSystemIcon{/// <summary>/// 依据文件名读取图标,若指定文件不存在,则返回空值。  /// </summary>/// <param name="fileName">文件路径</param>/// <param name="isLarge">是否返回大图标</param>/// <returns></returns>public static Icon GetIconByFileName(string fileName, bool isLarge = true){int[] phiconLarge = new int[1];int[] phiconSmall = new int[1];//文件名 图标索引 Win32.ExtractIconEx(fileName, 0, phiconLarge, phiconSmall, 1);IntPtr IconHnd = new IntPtr(isLarge ? phiconLarge[0] : phiconSmall[0]);if (IconHnd.ToString() == "0")return null;return Icon.FromHandle(IconHnd);}/// <summary>  /// 根据文件扩展名(如:.*),返回与之关联的图标。/// 若不以"."开头则返回文件夹的图标。  /// </summary>  /// <param name="fileType">文件扩展名</param>  /// <param name="isLarge">是否返回大图标</param>  /// <returns></returns>  public static 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, false);if (regVersion != null){regFileType = regVersion.GetValue("") as string;regVersion.Close();regVersion = Registry.ClassesRoot.OpenSubKey(regFileType + @"\DefaultIcon", false);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 { }return resultIcon;}}/// <summary>  /// 定义调用的API方法  /// </summary>  class Win32{[DllImport("shell32.dll")]public static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons);}

纸上得来终觉浅,绝知此事要躬行。

C# LISTVIEW实例:文件图标显示相关推荐

  1. office文件图标显示不正常

    最近,安装了XMLSpy后导致office文件图标显示不正常,变成Altova的图标,卸载xmlspy后,图标变成"未知文件"图标,强迫症犯了,必须改成正常的,网上查了好多资料,终 ...

  2. 桌面html文件图标异常,.htm .html文件图标显示异常的解决办法

    .htm .html文件图标显示异常的解决办法 发布时间:2008-11-22 12:55:25   作者:佚名   我要评论 症状:后缀为Htm和Html的文件图标显示为未关联的应用程序图标,(看着 ...

  3. deepin系统中.txt文件图标显示内容问题_深度系统Deepin 20最新正式版发布:从DDE到应用全面升级-Deepin 20,深度系统 ——快科技(驱动之家旗下媒体)-...

    距离Deepin 20正式版发布一个月后,社区版本迎来了第一次更新(1003). 本次从DDE到应用全面进行了升级,对桌面环境.应用进行功能优化和问题修复,同时本次也推送磁盘管理器.相机应用,邮件和手 ...

  4. win10安装Offic2016以后,Word文件、Excel文件、PPT文件图标显示不正常解决方法

    当安装Office以后,会因为注册表还是上一个软件的设定值,导致文件找不到对应的图标而无法正常显示,尤其在你上一个office软件用的是WPS的情况下,相关注册表的值会被修改为WPS图标值,但是当WP ...

  5. Excel,world ,ppt文件图标显示异常解决办法 (2022最新)

    分析原因: 卸载时勾选了"保留用户配置文件",导致WPS卸载后MS Office无法自动关联显示.doc.xls.ppt等文档的图标: 尝试过网上搜集的以下方法仍然无法解决问题: ...

  6. 解决安装office2016后文件图标显示空白图标

    适用Office2016/365中的Word.PowerPoint.Excel文件图标因安装WPS造成图标异常,显示为无法识别的程序图标,卸载.重装都不能修复,但能正常使用. 解决办法: 1.运行re ...

  7. deepin系统中.txt文件图标显示内容问题_深度系统Deepin 20最新正式版发布:全面升级_业界资讯...

    距离Deepin 20正式版发布一个月后,社区版本迎来了第一次更新(1003). 本次从DDE到应用全面进行了升级,对桌面环境.应用进行功能优化和问题修复,同时本次也推送磁盘管理器.相机应用,邮件和手 ...

  8. 关于doc、docx、PPT等文件图标显示与打开不正常的问题

    今天晚上临近电脑关机的时候,忽然发现桌面上的图标有些异样,仔细看了下,除了PDF文件其他几种office文件都显示不正常了,正如下图所示,看起来就像TXT文件的图标,实则是doc文件,并且打开的时候也 ...

  9. deepin系统中.txt文件图标显示内容问题_深度系统更新(deepin 20 1003)

    距离deepin 20正式版发布一个月后,社区版本迎来了第一次更新(1003).本次从DDE到应用全面进行了升级,对桌面环境.应用进行功能优化和问题修复,同时本次也推送磁盘管理器.相机应用,邮件和手机 ...

  10. 关于PPT嵌入对象文件图标显示为文字的方法(比如将系统图标换成文字)

    在做课件时,往往需要将其他文件链接到当前PPT中,比如数学中的解题步骤做在另外一个文件中,点击"解一""解二"时才显示该解题过程. 如图 在编辑好解题过程文件后 ...

最新文章

  1. 实现SSTab单个选项卡代码
  2. Java输入输出(io)流详解、图解
  3. JAVA Future
  4. 指定结构体元素的位字段
  5. C语言 文件的基本介绍
  6. 《新一代人工智能发展白皮书(2017年)》重磅发布(100页完整版PPT)
  7. Go range实现原理及性能优化剖析
  8. 苹果要弃用LCD屏,便宜的iPhone XR面临绝版
  9. NYOJ266 - 字符串逆序输出
  10. layer + ajax 弹出框
  11. 固定大小采样池中的随机采样证明
  12. Mybatis-学习笔记(3)mapper配置文件
  13. springboot快速搭建图书管理系统
  14. 商圈研究方案-调研内容及方法
  15. 10分钟搭建linux代理服务器
  16. Java常用英语汇总(面试必备)
  17. mian()方法详细分析(面向对象的体现)
  18. 订单可视化(智能制造、流程再造、企业信息化) 第三篇 订单可视化定义及目标
  19. Linux系统-LNMP部署农场牧场
  20. 基于mahout的动漫推荐系统

热门文章

  1. I.Algorithm Choosing Mushrooms
  2. uva 10099(最短路径)
  3. ARM9开发板实验笔记(1)
  4. 数据结构创建有向图(C++语言)
  5. QT installs的使用,编译时拷贝文件
  6. JavaSE_day11【内部类、注解】
  7. java.sql字符串拼成日期_sql字符串转换成日期
  8. 品牌如何正确联动B站UP主“恰饭视频”,最近一支不像恰饭视频的作品在B站火了
  9. 今有物不知其数三三数之JAVA_今有物不知其数.三三数之剩二.五五数之剩三.七七数之剩二.问物几何? 题目和参考答案——青夏教育精英家教网——...
  10. 简述窄带与宽带信号的区别