Getting Associated Icons Using VB .NET

Environment: VB.NET

Summary

This step-by-step article describes how to use the SHGetFileInfo function to get the icons that are associated with files.

Create a Windows Forms Application

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog box, click Visual Basic Projects under Project Types, and then click Windows Application under Templates.
  4. In the Name box, type GetIconSample.

Use the SHGetFileInfo Function

  1. Add the following code at the beginning of the Form1.vb file:
  2. Imports System.Runtime.InteropServices
  3. Add the following code in the Form1 class after the INHERITS statement:
  4. Private Structure SHFILEINFO
            Public hIcon As IntPtr            ' : icon
            Public iIcon As Integer           ' : icondex
            Public dwAttributes As Integer    ' : SFGAO_ flags
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
            Public szDisplayName As String
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
            Public szTypeName As String
    End Structure
    
    Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" _
            (ByVal pszPath As String, _
             ByVal dwFileAttributes As Integer, _
             ByRef psfi As SHFILEINFO, _
             ByVal cbFileInfo As Integer, _
             ByVal uFlags As Integer) As IntPtr
    
    Private Const SHGFI_ICON      = &H100
    Private Const SHGFI_SMALLICON = &H1
    Private Const SHGFI_LARGEICON = &H0    ' Large icon
    Private nIndex                = 0
    
  5. Add a listView control, a button control, and an imageList control to the form. The default names are ListView1, Button1, and ImageList1, respectively.
  6. In the Properties window of Button1, set the button text to Select a File, and then add the following code in the Button1_click event:
  7. Dim hImgSmall As IntPtr  'The handle to the system image list.
    Dim hImgLarge As IntPtr  'The handle to the system image list.
    Dim fName As String      'The file name to get the icon from.
    Dim shinfo As SHFILEINFO
    shinfo = New SHFILEINFO()
    Dim openFileDialog1 As OpenFileDialog
    openFileDialog1 = New OpenFileDialog()
    
    openFileDialog1.InitialDirectory = "c:\temp\"
    openFileDialog1.Filter           = "All files (*.*)|*.*"
    openFileDialog1.FilterIndex      = 2
    openFileDialog1.RestoreDirectory = True
    
    listView1.SmallImageList = imageList1
    listView1.LargeImageList = ImageList1
    
    shinfo.szDisplayName = New String(Chr(0), 260)
    shinfo.szTypeName    = New String(Chr(0), 80)
    
    If (openFileDialog1.ShowDialog() = DialogResult.OK) Then
         fName = openFileDialog1.FileName
    
         'Use this to get the small icon.
         hImgSmall = SHGetFileInfo(fName, 0, shinfo, _
                     Marshal.SizeOf(shinfo), _
                     SHGFI_ICON Or SHGFI_SMALLICON)
    
         'Use this to get the large icon.
         'hImgLarge = SHGetFileInfo(fName, 0,
         'ref shinfo, (uint)Marshal.SizeOf(shinfo),
         'SHGFI_ICON | SHGFI_LARGEICON);
    
         'The icon is returned in the hIcon member of the
         'shinfo struct.
         Dim myIcon As System.Drawing.Icon
         myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
    
         imageList1.Images.Add(myIcon)       'Add icon to
                                             'imageList.
    
         listView1.Items.Add(fName, nIndex)  'Add file name and
                                             'icon to listview.
         nIndex = nIndex + 1
    End If
    

Run The Project

  1. Compile the project: On the Build menu, click Build Solution.
  2. Press F5 to run the project.
  3. Click Select a File, and then select a file in the Open dialog box. The name of the file and the icon that are associated with the file appear in the ListView control.


Comments

  • Removed "Auto" for SH_TYPENAME and SH_DISPLAYNAME to work.

    Posted by Legacy on 02/10/2004 12:00am

    Originally posted by: Gerd Hoeren

    Thanks very much for this example. I needed to use
    
    SHGetFileInfo to get the file type and had to remove
    the "AUTO" modifier on your function definiton. That is:

    Private Declare Function SHGetFileInfo Lib "shell32.dll" ...

    Private Const SH_DISPLAYNAME As Integer = &H200
    Private Const SH_TYPENAME As Long = &H400

    ...

    SHGetFileInfo(fileInfo.FullName, FILE_ATTRIBUTE_NORMAL, shinfo, Marshal.SizeOf(shinfo), SH_TYPENAME)

    ____
    Gerd

    • That is nice

      Posted by parvezhakim12 on 12/30/2004 06:51am

      If it works then why to bother

      Reply
    • IMPORTANT

      Posted by parvezhakim12 on 12/12/2004 12:18pm

      This also belongs to Microsoft MSDN magzine. The idea of showing it here is to free developers from searching up the MSDN, and many changes have been made to that article where ever possible. This article is belongs to Microsoft and i do not have any intention to get involved in some legal problems, misuse it or distribute it. Parvez Ahmad

      Reply
    Reply
  • Use the SHGetFileInfo Function

    Posted by Legacy on 11/07/2003 12:00am

    Originally posted by: Lester Holt

    Excellent code and instructions.
    
    Thank You for great programmers!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Mobile is introducing sweeping changes throughout your workplace. As a senior stakeholder driving mobile initiatives in your organization, you may be lost in a sea of technologies and claims from vendors promising rapid delivery of applications to your employees, customers, and partners. To help explain some of the topics you will need to be aware of, and to separate the must-haves from the nice-to-haves, this reference guide can help you with applying a mobile strategy in the context of application …

  • In this on-demand webcast, Oracle ACE and Toad Product Architect Bert Scalzo discusses 10 powerful and hidden features in Toad® that help increase your productivity and DB performance. Watch this webcast today.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds