S
SStory
Hi all,
I really needed to get the icons associated with each file that I want to
show in a listview.
I used the follow modified code sniplets found on the internet.
I have left in commented code for anyone else who may be looking to do the
same.
I only care about the small icons in my program--detail view only.
Private Structure SHFileInfo ' Shell File Info structure
Public hIcon As IntPtr ' Icon handle
Public iIcon As Integer ' Icon index
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
'Gets the small icon for a file extension
Private Function GetIconViaExt(ByVal FileExt As String) As
System.Drawing.Icon
Const SHGFI_DISPLAYNAME = &H200
Const SHGFI_ICON = &H100
Const SHGFI_SMALLICON = &H1 ' Small icon
Const SHGFI_USEFILEATTRIBUTES = &H10 ' use passed dwFileAttribute
Const SHGFI_LARGEICON = &H0 ' Large icon
Const SHGFI_TYPENAME = &H400 ' get type name
Const DEFAULT_FLAGS = SHGFI_TYPENAME Or SHGFI_ICON Or SHGFI_SMALLICON Or
SHGFI_USEFILEATTRIBUTES
Const FILE_ATTRIBUTE_NORMAL = &H80
Dim hImg As IntPtr
Dim shinfo As New SHFileInfo
' Depending on the size, get the icon from the file
' If iSize = IconSize.Small Then ' Return small icon
hImg = SHGetFileInfo(FileExt, FILE_ATTRIBUTE_NORMAL, shinfo,
Marshal.SizeOf(shinfo), DEFAULT_FLAGS)
' ElseIf iSize = IconSize.Large Then ' Return large icon
' hImg = SHGetFileInfo(Filename, 0, shinfo, Marshal.SizeOf(shinfo),
SHGFI_ICON Or SHGFI_LARGEICON)
' End If
' strFileTypeName = shinfo.szTypeName
'The icon is returned in the hIcon member of the shinfo struct.
Debug.Write("calling GetIconViaExt....")
GetIconViaExt = System.Drawing.Icon.FromHandle(shinfo.hIcon)
Debug.WriteLine("....GetIconViaExt call returned")
End Function
from another method I do this
'fi is a fileinfo obj
Dim ico As Icon
ico = GetIconViaExt(fi.Extension)
'if GetIconViaExt returned something then lets add it to the image list
and remember it in the sorted array
If Not IsNothing(ico) Then
'add the icon to the image list
imlFileIcons.Images.Add(ico)
as you can see I get the filenames and then place them into an imagelist--I
actually keep up with them by another array and the extension to avoid
regetting the icon for the same extension once I have it, but anyhow....
The above code is some how trampling memory. It shows up in strange ways,
systemexecutionexception or even stackoverflowexceptions
commenting out this call makes it run fine so I know the problem is there.
Do I need to somehow release this icon pointer or something?
If so how can I add the icon I want to the imagelist, and release this icon
ptr to it and still have the image available in the imagelist?
Please help me solve this. My app really needs it if I will show icons.
If there is a simpler way that definitely works, please let me know.
I tried many before this one.
It works great except for this.
Thanks,
Shane
I really needed to get the icons associated with each file that I want to
show in a listview.
I used the follow modified code sniplets found on the internet.
I have left in commented code for anyone else who may be looking to do the
same.
I only care about the small icons in my program--detail view only.
Private Structure SHFileInfo ' Shell File Info structure
Public hIcon As IntPtr ' Icon handle
Public iIcon As Integer ' Icon index
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
'Gets the small icon for a file extension
Private Function GetIconViaExt(ByVal FileExt As String) As
System.Drawing.Icon
Const SHGFI_DISPLAYNAME = &H200
Const SHGFI_ICON = &H100
Const SHGFI_SMALLICON = &H1 ' Small icon
Const SHGFI_USEFILEATTRIBUTES = &H10 ' use passed dwFileAttribute
Const SHGFI_LARGEICON = &H0 ' Large icon
Const SHGFI_TYPENAME = &H400 ' get type name
Const DEFAULT_FLAGS = SHGFI_TYPENAME Or SHGFI_ICON Or SHGFI_SMALLICON Or
SHGFI_USEFILEATTRIBUTES
Const FILE_ATTRIBUTE_NORMAL = &H80
Dim hImg As IntPtr
Dim shinfo As New SHFileInfo
' Depending on the size, get the icon from the file
' If iSize = IconSize.Small Then ' Return small icon
hImg = SHGetFileInfo(FileExt, FILE_ATTRIBUTE_NORMAL, shinfo,
Marshal.SizeOf(shinfo), DEFAULT_FLAGS)
' ElseIf iSize = IconSize.Large Then ' Return large icon
' hImg = SHGetFileInfo(Filename, 0, shinfo, Marshal.SizeOf(shinfo),
SHGFI_ICON Or SHGFI_LARGEICON)
' End If
' strFileTypeName = shinfo.szTypeName
'The icon is returned in the hIcon member of the shinfo struct.
Debug.Write("calling GetIconViaExt....")
GetIconViaExt = System.Drawing.Icon.FromHandle(shinfo.hIcon)
Debug.WriteLine("....GetIconViaExt call returned")
End Function
from another method I do this
'fi is a fileinfo obj
Dim ico As Icon
ico = GetIconViaExt(fi.Extension)
'if GetIconViaExt returned something then lets add it to the image list
and remember it in the sorted array
If Not IsNothing(ico) Then
'add the icon to the image list
imlFileIcons.Images.Add(ico)
as you can see I get the filenames and then place them into an imagelist--I
actually keep up with them by another array and the extension to avoid
regetting the icon for the same extension once I have it, but anyhow....
The above code is some how trampling memory. It shows up in strange ways,
systemexecutionexception or even stackoverflowexceptions
commenting out this call makes it run fine so I know the problem is there.
Do I need to somehow release this icon pointer or something?
If so how can I add the icon I want to the imagelist, and release this icon
ptr to it and still have the image available in the imagelist?
Please help me solve this. My app really needs it if I will show icons.
If there is a simpler way that definitely works, please let me know.
I tried many before this one.
It works great except for this.
Thanks,
Shane