【转】 C# ListView实例:文件图标显示
说明:本例将目录中的文件显示在窗体的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实例:文件图标显示的更多相关文章
- 解决Chrome关联Html文件图标显示为空白
用记事本保存为ChromeHTML.reg Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\CLSID\{42042206-2D85-1 ...
- 修复TortoiseGit文件夹和文件图标不显示
原文:http://blog.moocss.com/tutorials/git/1823.html 一. 我的运行环境: 操作系统 Windows 7/8 32bit TortoiseGit (1.7 ...
- TortoiseSVN文件夹及文件图标不显示解决方法(兼容Window xp、window7)
最近遇到TortoiseSVN图标(如上图:增加文件图标.文件同步完成图标等)不显示问题,网上找到的解决方法试了很多都无法真正解决,最后总结了一下,找到了终极解决方案,当然此方案也有弊端,接下来我们就 ...
- svn图标显示不正常,文件夹显示但文件不显示svn图标
svn图标显示不正常,文件夹显示但文件不显示svn图标 这个问题的引发是自己造成的,使用myEclipse时progress会卡在 refresh svn status cache (0%)这里, ...
- Eclipse或MyEclipse没有在java类文件上显示Spring图标的问题
Eclipse或MyEclipse没有在java类文件上显示接口图标的问题解决办法: 前: 后:
- Android Studio 那些事|Activity文件前标识图标显示为 j 而是 c
问题:Activity文件前标识图标显示为 j 而是 c 的图标,或是没有显示,并且自己主动提示不提示 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/fo ...
- Git文件不显示图标/标识
初次使用Git服务功能,做了很多探路事情,记录下刚刚遇到的问题 情况:安装了Git应用程序,或者也安装了TortoiseGit-1.8.16.0-64bit(类似SVN工具)后,上传下载文件没有问题, ...
- SVN检出后文件没有图标显示
SVN检出后文件没有图标显示 "Win + R"打开运行框,输入"regedit"打开注册表 在注册表编辑界面按"Ctrl + F"快捷 ...
- 预装的Office2016,文件图标表显示以及新建失败问题解决 方法
新购买笔记本电脑,预装的office2016 学生版 启动激活后,会出现文件图标异常, 文件的类型为: ms-resource:Strings/FtaDisplayName.docx (.docx) ...
随机推荐
- 【Web API系列教程】3.4 — 实战:处理数据(处理实体关系)
前言 本部分描写叙述了EF怎样载入相关实体的细节,而且怎样在你的模型类中处理环形导航属性.(本部分预备了背景知识,而这不是完毕这个教程所必须的.你也能够跳到第五节) 预载入和延迟载入 预载入和延迟载入 ...
- SqlHelper——仅仅由于在人群中多看了你一眼
一.SqlHelper 出场 不是由于大家都在用SqlHelper所以才用,是由于连接数据库关闭数据库查询数据库的多了也就加上了SqlHelper.当你的非常多需求都有一个同样的方法的时候我们没有必要 ...
- TCP打洞技术
//转http://iamgyg.blog.163.com/blog/static/3822325720118202419740/ 建立穿越NAT设备的p2p的TCP连接仅仅比UDP复杂一点点,TCP ...
- spark transform系列__sortByKey
该函数主要功能:通过指定的排序规则与进行排序操作的分区个数,对当前的RDD中的数据集按KEY进行排序,并生成一个SHUFFLEdrdd的实例,这个过程会运行shuffle操作,在运行排序操作前,sor ...
- svn 服务器的搭建
SVN服务器运行模式:模式1:svn服务器单独运行 监听: 3690端口 访问: svn://IP模式2: svn 服务器+ apache : 80 端口 访问: http://IP ...
- DB-MySQL:MySQL 临时表
ylbtech-DB-MySQL:MySQL 临时表 1.返回顶部 1. MySQL 临时表 MySQL 临时表在我们需要保存一些临时数据时是非常有用的.临时表只在当前连接可见,当关闭连接时,Mysq ...
- OC中的类扩展
类扩展 是在原有类的基础扩展一个新的属性和对象方法 但是方法的实现还是要写在原有的声明中,不然是不会被访问到的 类扩展可以扩展在新的头文件中,然后在主函数中导入. 利用类扩展可以变相的实现属性的私有化 ...
- 常用相关linux命令
查看进程netstat -tnlp | egrep "(9097)" lsof -i:9097 ps -ef | grep kafka 观察句柄变化lsof -p $pid | w ...
- ffmpeg键盘命令响应程序详解
一.对终端进行读写 当一个程序在命令提示符中被调用时, shell负责将标准输入和标准输出流连接到你的程序, 实现程序与用户间的交互. 1. 标准模式和非标准模式 在默认情况下, 只有用户按下回车 ...
- FluentAPI配置
基本 EF 配置只要配置实体类和表.字段的对应关系.表间关联关系即可. 如何利用 EF的高级配置,达到更多效果:如果数据错误(比如字段不能为空.字符串超长等),会在 EF 层就会报错,而不会被提交给数 ...