Is there any windows form control that shows list of drive letters with icons?
A:
If you are willing to pay for this, you could check out http://viewpack.qarchive.org/.
I'm not aware of any free controls.
Patrick McDonald
2009-08-05 10:51:55
+1
A:
No, but I'm sure you could make it happen, shouldn't be too tricky, either with a TreeView or if you would just like the list then you could use a ListView.
The code to get the drives would be similar to this:
//Get all Drives
DriveInfo[] ListAllDrives = DriveInfo.GetDrives();
To determine the icons for the ListViewItem or TreeViewNodes you could do something like this:
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.)
}
ThePower
2009-08-05 12:31:24
A:
I've finally came up with my own control.
I fill a listview with drives as follows:
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);
}
Where GetFileIcon is my own method that calls 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 class is copied as is form this site and looks as follows:
[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);
}
I hope it will help someone.
Piotr Czapla
2009-08-05 12:31:28
Just a note, watch out when using shell32.dll, it's liable to change (you know what microsoft are like!), I asked a question about using their icons here http://stackoverflow.com/questions/949196/loading-icons-from-shell32-dll-win32-handle-is-not-valid-or-is-the-wrong-type
ThePower
2009-08-05 12:42:59
Yeep it works on vista and doesn't on XP
Piotr Czapla
2009-08-05 13:33:16