资 源 简 介
C# 根据文件名提取文件类型图标,设定好文件目录后,本例中是读取C:Windows下的所有文件,并根据文件类型自动显示图标,如测试图所示,将文件类型的图标添加到listView中,下面是具体的实现代码:
this.imageList1.Images.Clear();
this.listView1.Items.Clear();
string MyFolder = @"C:Windows";
DirectoryInfo MyDir = new DirectoryInfo(MyFolder);
ListViewItem MyItem;
this.listView1.BeginUpdate();
foreach (FileInfo MyFile in MyDir.GetFiles())
{
Icon MyIcon = SystemIcons.WinLogo;
MyItem = new ListViewItem(MyFile.Name, 1);
MyIcon = Icon.ExtractAssociatedIcon(MyFile.FullName);
if (!this.imageList1.Images.ContainsKey(MyFile.Extension))
{
MyIcon =Icon.ExtractAssociatedIcon(MyFile.FullName);
this.imageList1.Images.Add(MyFile.Extension, MyIcon);
}
MyItem.ImageKey = MyFile.Extension;
this.listView1.Items.Add(MyItem);
}