获取系统中可用的驱动器列表

实现

using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
    public IntPtr hIcon;
    public int iIcon;
    public int dwAttributes;
    public string szDisplayName;
    public string szTypeName;
}

[DllImport("shell32")]
private static extern int SHGetFileInfo(string pszPath, int dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, int uFlags);

const int SHGFI_ICON = 0x0100;
const int SHGFI_LARGEICON = 0x0000;

static string[] drives;
ImageList img = new ImageList();

获得列表

private void Form1_Load(object sender, System.EventArgs e)
{
    this.listView1.LargeImageList = img;
    this.listView1.SmallImageList = 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);
    }
}
Contributors: FHL