none
Local units RRS feed

  • Question

  • Good…
    First of all, I tell you my problem ...
    1.- I would like to list the local, removable units using Shell API functions.
    2.- Get your properties such as, name, icon ...
    3.- Show the result obtained in a SUBMENUITEM of MenuStrip.
    Example, similar to Windows 7, as in the following image:

    4.- When clicking on a SUBMENUITEM, the contents of the unit are displayed in a listview control.


    Well I hope you help me with these complicated operations.
    Thank you.

    Tuesday, April 3, 2018 3:37 PM

Answers

  • You can get the drives with DriveInfo.GetDrives

    You can get the icons with SHGetFileInfo

    Dim sfi As New SHFILEINFO
    Dim pRet As IntPtr = SHGetFileInfo(d.Name, 0, sfi, Marshal.SizeOf(sfi), SHGFI_ICON Or SHGFI_SMALLICON)

    with declarations :

    Public Structure SHFILEINFO
        Public hIcon As IntPtr
        Public iIcon As Integer
        Public dwAttributes As Integer
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public szDisplayName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> Public szTypeName As String
    End Structure
    
    Public Const SHGFI_ICON = &H100
    Public Const SHGFI_SMALLICON = &H1
    
    <DllImport("Shell32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Public Shared Function SHGetFileInfo(pszPath As String, dwFileAttributes As UInteger, ByRef psfi As SHFILEINFO, cbFileInfo As UInteger, uFlags As UInteger) As IntPtr
    End Function


    • Edited by Castorix31 Tuesday, April 3, 2018 4:44 PM
    • Proposed as answer by Mr. Monkeyboy Wednesday, April 4, 2018 1:32 AM
    • Marked as answer by JenCarlos Wednesday, April 4, 2018 7:49 PM
    Tuesday, April 3, 2018 4:41 PM
  •  You can also look at the Vb.Net IconHelper class that I posted at the link below.  It can be used to get Bitmaps of disk drive icons or file icons.  You just pass it a String of a  drive letter,  full file path,  or just an extension of a registered file type and specify which icon size you want (32x32 or 16x16).  It uses the same api as Castorix31 has shown you.

    Get Icon Images Of Files, Folders, Drives, or Registered Files


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Wednesday, April 4, 2018 12:55 AM
    • Marked as answer by JenCarlos Wednesday, April 4, 2018 7:50 PM
    Wednesday, April 4, 2018 12:06 AM
  • Good…
    First of all, I tell you my problem ...
    1.- I would like to list the local, removable units using Shell API functions.
    2.- Get your properties such as, name, icon ...
    3.- Show the result obtained in a SUBMENUITEM of MenuStrip.
    Example, similar to Windows 7, as in the following image:

    4.- When clicking on a SUBMENUITEM, the contents of the unit are displayed in a listview control.


    Well I hope you help me with these complicated operations.
    Thank you.

    You should be able to find code in this forum for a ListView control for displaying contents of a drive. Although a TreeView control would be much better for directory/folder branching IMO and there is code for that in this Forum too if you search for it. Though it will take some learning on your part to use these capabilities.

    List directories in treeview

    Display Folders and Files in TreeView and ListBox

    Show folders/files in TreeView

    VB.Net Forum search results for "Treeview" selecting Most recent thread


    La vida loca

    • Edited by Mr. Monkeyboy Wednesday, April 4, 2018 1:41 AM
    • Marked as answer by JenCarlos Wednesday, April 4, 2018 7:49 PM
    Wednesday, April 4, 2018 1:35 AM

All replies

  • You can get the drives with DriveInfo.GetDrives

    You can get the icons with SHGetFileInfo

    Dim sfi As New SHFILEINFO
    Dim pRet As IntPtr = SHGetFileInfo(d.Name, 0, sfi, Marshal.SizeOf(sfi), SHGFI_ICON Or SHGFI_SMALLICON)

    with declarations :

    Public Structure SHFILEINFO
        Public hIcon As IntPtr
        Public iIcon As Integer
        Public dwAttributes As Integer
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public szDisplayName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> Public szTypeName As String
    End Structure
    
    Public Const SHGFI_ICON = &H100
    Public Const SHGFI_SMALLICON = &H1
    
    <DllImport("Shell32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Public Shared Function SHGetFileInfo(pszPath As String, dwFileAttributes As UInteger, ByRef psfi As SHFILEINFO, cbFileInfo As UInteger, uFlags As UInteger) As IntPtr
    End Function


    • Edited by Castorix31 Tuesday, April 3, 2018 4:44 PM
    • Proposed as answer by Mr. Monkeyboy Wednesday, April 4, 2018 1:32 AM
    • Marked as answer by JenCarlos Wednesday, April 4, 2018 7:49 PM
    Tuesday, April 3, 2018 4:41 PM
  •  You can also look at the Vb.Net IconHelper class that I posted at the link below.  It can be used to get Bitmaps of disk drive icons or file icons.  You just pass it a String of a  drive letter,  full file path,  or just an extension of a registered file type and specify which icon size you want (32x32 or 16x16).  It uses the same api as Castorix31 has shown you.

    Get Icon Images Of Files, Folders, Drives, or Registered Files


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Wednesday, April 4, 2018 12:55 AM
    • Marked as answer by JenCarlos Wednesday, April 4, 2018 7:50 PM
    Wednesday, April 4, 2018 12:06 AM
  • Good…
    First of all, I tell you my problem ...
    1.- I would like to list the local, removable units using Shell API functions.
    2.- Get your properties such as, name, icon ...
    3.- Show the result obtained in a SUBMENUITEM of MenuStrip.
    Example, similar to Windows 7, as in the following image:

    4.- When clicking on a SUBMENUITEM, the contents of the unit are displayed in a listview control.


    Well I hope you help me with these complicated operations.
    Thank you.

    You should be able to find code in this forum for a ListView control for displaying contents of a drive. Although a TreeView control would be much better for directory/folder branching IMO and there is code for that in this Forum too if you search for it. Though it will take some learning on your part to use these capabilities.

    List directories in treeview

    Display Folders and Files in TreeView and ListBox

    Show folders/files in TreeView

    VB.Net Forum search results for "Treeview" selecting Most recent thread


    La vida loca

    • Edited by Mr. Monkeyboy Wednesday, April 4, 2018 1:41 AM
    • Marked as answer by JenCarlos Wednesday, April 4, 2018 7:49 PM
    Wednesday, April 4, 2018 1:35 AM