You are currently offline, waiting for your internet to reconnect

How to use the SHGetFileInfo function to get the icons that are associated with files in Visual Basic .NET

This article was previously published under Q319340
This article has been archived. It is offered "as is" and will no longer be updated.
For a Microsoft Visual C# .NET version of this article, see 319350.
SUMMARY
This step-by-step article describes how to use the SHGetFileInfo function to get the icons that are associated with files.

back to the top

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.
back to the top

Use the SHGetFileInfo function

  1. Add the following code at the beginning of the Form1.vb file.
    Imports System.Runtime.InteropServices					
  2. Add the following code in the Form1 class after the INHERITS statement.
    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 StringEnd StructurePrivate Declare Ansi 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 IntPtrPrivate Const SHGFI_ICON = &H100Private Const SHGFI_SMALLICON = &H1Private Const SHGFI_LARGEICON = &H0         ' Large iconPrivate nIndex = 0					
  3. Add a listView control, a button control, and an imageList control to the form. The default names are ListView1, Button1, and ImageList1 respectively.
  4. 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:
    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 SHFILEINFOshinfo = New SHFILEINFO()Dim openFileDialog1 As OpenFileDialogopenFileDialog1 = New OpenFileDialog()openFileDialog1.InitialDirectory = "c:\temp\"openFileDialog1.Filter = "All files (*.*)|*.*"openFileDialog1.FilterIndex = 2openFileDialog1.RestoreDirectory = TruelistView1.SmallImageList = imageList1listView1.LargeImageList = ImageList1shinfo.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 + 1End If					
back to the top

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 is associated with the file appear in the ListView control.
back to the top
REFERENCES
For additional information in a Microsoft Visual C# .NET version of this article, click the article number below to view the article in the Microsoft Knowledge Base:
319350 How To Use the SHGetFileInfo Function To Get the Icons That Are Associated with Files in Visual C# .NET
back to the top
Properties

Article ID: 319340 - Last Review: 12/07/2015 09:15:41 - Revision: 2.0

Microsoft Visual Studio .NET 2002 Professional Edition, Microsoft Visual Studio .NET 2002 Enterprise Architect, Microsoft Visual Studio .NET 2002 Enterprise Developer, Microsoft Visual Studio .NET 2002 Academic Edition

  • kbnosurvey kbarchive kbhowtomaster KB319340
Feedback