【转】 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) ...
随机推荐
- oracle(cast , to_char , to_date )用法
cast : cast(要转换的值 AS 转换的类型) From To BINARY_FLOAT, BINARY_DOUBLE To CHAR, VARCHAR2 To NUMBER To DATET ...
- Eclipse-去除空白行
CTRL+F Find: ^\s*\n 注意前后不要有空白 Replace With: 为空,不填 勾选:Regular expressions 正则表达式 替 ...
- Quartz.Net 使用心得(一)
最近工作内容与定时任务相关,在实际使用Quartz过程中,有两个小问题较为困扰. 一.多个Trigger如何触发一个Job. 比如上下班打卡时推送消息,上班时间为9:30,打卡提醒时间为9:20较好. ...
- HDU 4349
想了好久,没思路.看别人说是卢卡斯,就去看卢卡斯了,看完卢卡斯,再用它推导一下,很容易就知道,答案是2^n的二进制中一的个数.改天找个时间写个卢卡斯的总结.~~~今晚竟然要上形势政治课,靠.... # ...
- 大菲波数 【杭电-HDOJ-1715】 附题+具体解释
/* 大菲波数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- HDU 4893 线段树裸题
Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- Llama-impala on yarn的中间协调服务
本文基于CDH发行版下的Hadoop Yarn和Impala 早期的Impala版本号中.为了使用Impala.我们一般会在以Client/Server的结构在各个集群节点启动impala-serve ...
- LeetCode题解 || Longest Substring Without Repeating Characters (O(n)算法)问题
problem: Given a string, find the length of the longest substring without repeating characters. For ...
- HDU 1269 -- 迷宫城堡【有向图求SCC的数目 && 模板】
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- POJ 1236--Network of Schools【scc缩点构图 && 求scc入度为0的个数 && 求最少加几条边使图变成强联通】
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13325 Accepted: 53 ...