Microsoft KB Archive/319350

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

Article ID: 319350

Article Last Modified on 9/22/2006

-

APPLIES TO


 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * 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

-



This article was previously published under Q319350



IN THIS TASK
SUMMARY
 * Create a Windows Forms application
 * Use the SHGetFileInfo function
 * Run the project

REFERENCES



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 C# 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
  Add the following code in the Form1.cs file at the end of the USING statements. using System.Runtime.InteropServices;   Add the following code after the Form1 class of the GetIconSample namespace. [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(&quot;shell32.dll&quot;)] public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags); }                     Add the following code in the Form1 class after the PRIVATE statements: private int nIndex = 0;  Add a listView control, a button control, and an imageList control to the form. The default names are listView1, button1, and imageList1 respectively.  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: IntPtr hImgSmall; //the handle to the system image list IntPtr hImgLarge; //the handle to the system image list string fName; // 'the file name to get icon from SHFILEINFO shinfo = new SHFILEINFO; OpenFileDialog openFileDialog1 = new OpenFileDialog; openFileDialog1.InitialDirectory = &quot;c:\\temp\\&quot;; openFileDialog1.Filter = &quot;All files (*.*)|*.*&quot;; openFileDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true ;

listView1.SmallImageList = imageList1; listView1.LargeImageList = imageList1; if(openFileDialog1.ShowDialog == DialogResult.OK) {    fName = openFileDialog1.FileName; //Use this to get the small Icon hImgSmall = Win32.SHGetFileInfo(fName, 0, ref shinfo,(uint)Marshal.SizeOf(shinfo),Win32.SHGFI_ICON |Win32.SHGFI_SMALLICON);

//Use this to get the large Icon //hImgLarge = SHGetFileInfo(fName, 0,    //  ref shinfo, (uint)Marshal.SizeOf(shinfo),     //  Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);

//The icon is returned in the hIcon member of the shinfo struct System.Drawing.Icon myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon); imageList1.Images.Add(myIcon); //Add file name and icon to listview listView1.Items.Add(fName, nIndex++); }                   

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

