2009-08-05 67 views

回答

3

不,但我相信你可以做到这一点,不应该太棘手,无论是使用TreeView,或者如果你只是喜欢列表,然后你可以使用ListView。

的代码来获取驱动器将与此类似:

//Get all Drives 
DriveInfo[] ListAllDrives = DriveInfo.GetDrives(); 

要确定一个ListViewItem或TreeViewNodes你可以做这样的事情的图标:

foreach (DriveInfo Drive in ListAllDrives) 
{ 
    //Create ListViewItem, give name etc. 
    ListViewItem NewItem = new ListViewItem(); 
    NewItem.Text = Drive.Name; 

    //Check type and get icon required. 
    if (Drive.DriveType.Removable) 
    { 
    //Set Icon as Removable Icon 
    }  
    //else if (Drive Type is other... etc. etc.) 
} 
0

我终于想出了我自己的控制。

我填一个ListView与驱动如下:

listView1.SmallImageList = new ImageList(); 
var drives = DriveInfo.GetDrives() 
         .Where(x => x.DriveType == DriveType.Removable) 
         .Select(x => x.Name.Replace("\\","")); 
foreach (var driveName in drives) 
{ 
    listView1.SmallImageList.Images.Add(driveName, GetFileIcon(driveName)); 
    listView1.Items.Add(driveName, driveName); 
} 

哪里GetFileIcon是我自己的方法调用的SHGetFileInfo:

IntPtr hImgSmall; //the handle to the system image list 
SHFILEINFO shinfo = new SHFILEINFO(); 
//Use this to get the small Icon 
hImgSmall = Win32.SHGetFileInfo(fileName, 0, ref shinfo, 
           (uint)Marshal.SizeOf(shinfo), 
           Win32.SHGFI_ICON | 
           Win32.SHGFI_SMALLICON); 

return System.Drawing.Icon.FromHandle(shinfo.hIcon); 

的Win32类原样复制形式this site,看起来如下:

[StructLayout(LayoutKind.Sequential)] 
public struct SHFILEINFO 
{ 
    public IntPtr hIcon; 
    public IntPtr iIcon; 
    public uint dwAttributes; 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] 
    public string szDisplayName; 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] 
    public string szTypeName; 
}; 

class Win32 
{ 
    public const uint SHGFI_ICON = 0x100; 
    public const uint SHGFI_LARGEICON = 0x0; // 'Large icon 
    public const uint SHGFI_SMALLICON = 0x1; // 'Small icon 

    [DllImport("shell32.dll")] 
    public static extern IntPtr SHGetFileInfo(string pszPath, 
     uint dwFileAttributes, 
     ref SHFILEINFO psfi, 
     uint cbSizeFileInfo, 
     uint uFlags); 
} 

我希望它能帮助别人。

+1

只需注意,使用shell32.dll时要小心,它可能会改变(你知道微软是什么样的!),我问了一个关于在这里使用它们的图标的问题http://stackoverflow.com/questions/949196/加载图标从shell32-dl​​l-win32-handle-is-valid-or-is-the-wrong-type – ThePower 2009-08-05 12:42:59