1. 新建一个工程demo
2.添加一个ListView控件
3.添加以下引用:
using System.Runtime.InteropServices;
[DllImport("shell32")]
private static extern int SHGetFileInfo(string pszPath,int dwFileAttributes,ref SHFILEINFO psfi,int cbFileInfo,int uFlags);4.添加一些变量。
const int SHGFI_ICON = 0x0100;
const int SHGFI_LARGEICON = 0x0000;
static string[] drives;
ImageList img=new ImageList();5.添加窗体的Load方法:
private void Form1_Load(object sender, System.EventArgs e)
...{
this.listView1.LargeImageList=img;
// this.listView1.SmallImageList=img;
this.listView1.LargeImageList=img;
this.listView1.StateImageList=img;
drives=Environment.GetLogicalDrives();
for(int i=0;i<drives.Length;i++)
...{
string str_temp=drives[i];
this.listView1.Items.Add(str_temp);
this.listView1.Items[i].ImageIndex=i;
}
for(int i=0;i<drives.Length;i++)
...{
SHFILEINFO FileInfo=new SHFILEINFO();
SHGetFileInfo(drives[i],0,ref FileInfo,Marshal.SizeOf(FileInfo),SHGFI_ICON | SHGFI_LARGEICON);
Icon myIcon;
myIcon=Icon.FromHandle(FileInfo.hIcon);
img.Images.Add(myIcon);
}
}
本文介绍如何在C#应用程序中使用ListView显示计算机磁盘驱动器及其图标。通过调用SHGetFileInfo函数获取磁盘图标,并将其添加到ImageList中,最后设置ListView的Items以显示磁盘名称和图标。

被折叠的 条评论
为什么被折叠?



