Get associated icon for a file

T

techspirit

Hi all,

1) I found out from the vb.net group that it is possible to get an
associated icon for a file. My requirement is to display all programs
from the Start Menu for the user along with the associated icons. While
I am able to list the programs correctly, the icons that get displayed
alongside are the ones for the folders.
The form has listview and imagelist controls on it. I shall paste the
code here.
2) I was also wondering why this piece of code ,
Dim FileProperties As FileVersionInfo =
FileVersionInfo.GetVersionInfo(f)
strfDesc = FileProperties.FileDescription
returns the description for an executable file only. I am trying to
display the program description instead of the the path for the files.
Please help. Thanks in advance.
'***********************************************************************************************
Imports System.Runtime.InteropServices
Imports System.IO

Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ListView1 As System.Windows.Forms.ListView
Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
Friend WithEvents ColumnHeader1 As
System.Windows.Forms.ColumnHeader
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.Button1 = New System.Windows.Forms.Button
Me.ListView1 = New System.Windows.Forms.ListView
Me.ImageList1 = New
System.Windows.Forms.ImageList(Me.components)
Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(176, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(88, 24)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'ListView1
'
Me.ListView1.Columns.AddRange(New
System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1})
Me.ListView1.Location = New System.Drawing.Point(8, 40)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(816, 368)
Me.ListView1.TabIndex = 1
Me.ListView1.View = System.Windows.Forms.View.Details
'
'ImageList1
'
Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)
Me.ImageList1.TransparentColor =
System.Drawing.Color.Transparent
'
'ColumnHeader1
'
Me.ColumnHeader1.Width = 812
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(872, 438)
Me.Controls.Add(Me.ListView1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)

End Sub

#End Region
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 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 IntPtr

Private Const SHGFI_ICON = &H100
Private Const SHGFI_SMALLICON = &H1
Private Const SHGFI_LARGEICON = &H0 ' Large icon
Private nIndex = 0



Dim intI As Integer = 0



'***********************************************************************************
'Retrieve and set the large and small ImageLists for ListView1 at
form load


'***********************************************************************************

Sub DirSearch(ByVal sDir As String, ByVal obListView As ListView)
Dim d As String
Dim f As String


'-------------------------------------------------------
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 New SHFILEINFO
shinfo = New SHFILEINFO

Dim SHI As New SHFILEINFO

ListView1.SmallImageList = ImageList1
ListView1.LargeImageList = ImageList1

shinfo.szDisplayName = New String(Chr(0), 260)
shinfo.szTypeName = New String(Chr(0), 80)
'----------------------------------------------------------
Dim lvItem As ListViewItem
'ListView item.
Dim myIcon As System.Drawing.Icon
Dim strfDesc As String

Try
For Each d In Directory.GetDirectories(sDir)
For Each f In Directory.GetFiles(d, "*.*")
intI = intI + 1
With obListView
.BeginUpdate()


''---------------------------------------------------
'Use this to get the small icon.
hImgSmall = SHGetFileInfo(fName, 0, shinfo,
Marshal.SizeOf(shinfo), _
SHGFI_ICON Or SHGFI_SMALLICON)
myIcon =
System.Drawing.Icon.FromHandle(shinfo.hIcon)


ImageList1.Images.Add(myIcon) 'Add icon to
imageList.


''---------------------------------------------------
Dim FileProperties As FileVersionInfo =
FileVersionInfo.GetVersionInfo(f)
strfDesc = FileProperties.FileDescription

.Items.Add(f, nindex)
nIndex = nIndex + 1
.EndUpdate()
End With
Next
DirSearch(d, obListView)
Next
Catch excpt As System.Exception
Debug.WriteLine(excpt.Message)
End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim strWinDir As String


strWinDir =
Environment.GetFolderPath(Environment.SpecialFolder.StartMenu)

DirSearch(strWinDir, ListView1)

End Sub
End Class
 
H

Herfried K. Wagner [MVP]

techspirit said:
2) I was also wondering why this piece of code ,
Dim FileProperties As FileVersionInfo =
FileVersionInfo.GetVersionInfo(f)
strfDesc = FileProperties.FileDescription
returns the description for an executable file only. I am trying to
display the program description instead of the the path for the files.

Determining the description of a file's file type
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=getfiledescription&lang=en>
 
T

techspirit

Hi,
Thanks.
I did try using the code sample from codeguru. I need to repeatedly
search all subdirectories under the Start Menu and display the programs
the current user can view/execute. Mainly, I also need to display the
result as file names or description , rather than the full paths which
these code samples result in.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top