Código:
Public Shared Function GetIcon(ByVal path As String, ByVal tipo As TipoIcon) As System.Drawing.Icon
Dim hImgSmall As IntPtr 'The handle to the system image list.
Dim myIcon As System.Drawing.Icon
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()
shinfo.szDisplayName = New String(Chr(0), 260)
shinfo.szTypeName = New String(Chr(0), 80)
fName = path
'Use this to get the small icon.
Select Case tipo
Case TipoIcon.Small
hImgSmall = SHGetFileInfo(fName, 0, shinfo, Marshal.SizeOf(shinfo), _
SHGFI_ICON Or SHGFI_SMALLICON)
Case TipoIcon.Large
hImgLarge = SHGetFileInfo(fName, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_LARGEICON)
End Select
' Try
myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
' Catch ex As Exception
' 'Erorr
'End Try
Return myIcon
End Function
Dim hImgSmall As IntPtr 'The handle to the system image list.
Dim myIcon As System.Drawing.Icon
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()
shinfo.szDisplayName = New String(Chr(0), 260)
shinfo.szTypeName = New String(Chr(0), 80)
fName = path
'Use this to get the small icon.
Select Case tipo
Case TipoIcon.Small
hImgSmall = SHGetFileInfo(fName, 0, shinfo, Marshal.SizeOf(shinfo), _
SHGFI_ICON Or SHGFI_SMALLICON)
Case TipoIcon.Large
hImgLarge = SHGetFileInfo(fName, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_LARGEICON)
End Select
' Try
myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
' Catch ex As Exception
' 'Erorr
'End Try
Return myIcon
End Function
La funcion funcina correctamente
Luego ago esto
Código:
Dim ImageL As New ImageList
Dim dirsF() As FileInfo = New DirectoryInfo("C:\").GetFiles
ListI.LargeImageList = ImageL
For Each Dir As FileInfo In dirsF
Dim Nod As New ListViewItem
Nod.Text = Dir.Name
ImageL.Images.Add(InformacíonSecurity.GetIcon(Dir.FullName, InformacíonSecurity.TipoIcon.Small))
Nod.ImageIndex = ImageL.Images.Count - 1
ListI.Items.Add(Nod)
Next
Dim dirsF() As FileInfo = New DirectoryInfo("C:\").GetFiles
ListI.LargeImageList = ImageL
For Each Dir As FileInfo In dirsF
Dim Nod As New ListViewItem
Nod.Text = Dir.Name
ImageL.Images.Add(InformacíonSecurity.GetIcon(Dir.FullName, InformacíonSecurity.TipoIcon.Small))
Nod.ImageIndex = ImageL.Images.Count - 1
ListI.Items.Add(Nod)
Next
Eso tambine funciona pero los iconos me parecen muy pequeños y si cambio el tamaño del imagelist se ve el icono con muy mala calidad y quero verlos grandes corectamente como pasa con explorer .
Gracias por vuestro tiempo