Moin zusammen,
ich möchte in meinem Listview Datenverzeichnisse von der Festplatte anzeigen. Dazu gehören natürlich auch die Icons dazu. Im Internet habe ich einige Sachen gefunden, wie man Icons von Dateien ermittelt. Leider will das aber bei mir nicht funktionieren.
Wo liegt hier in meinem Code der Fehler?
Zum testen:
einfach auf einem Formular ein Button und ein Listview setzen. Desweiteren ein Modul hinzufügen (jaja, ich weis, Module sind "out" ) Habe diese aus dem Internet pro Forma erst einmal übernommen, um zu schauen, ob es überhaupt klappt. Hier der Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
Dim Path = "G:\_Testen_--\Anderes"
Dim stFiles() As String 'Retrieve all
' files
' from path stPath.
Try
stFiles = Directory.GetFiles(Path)
Array.Sort(stFiles)
Dim SHI As New SHFILEINFO() 'SHFILEINFO
' structure.
Dim SHISize As Integer = Marshal.SizeOf(SHI) 'Size of SHI.
Dim uflags As SHGFI = SHGFI.SMALLICON Or SHGFI.ICON 'Flags for SHI.
Dim shLoop As Short 'Loop variable.
Dim ltvItem As ListViewItem 'ListView
' item.
Dim datei As FileInfo
With ListView1
.BeginUpdate()
.Items.Clear()
For shLoop = 0 To stFiles.Length - 1
datei = New FileInfo(stFiles(shLoop))
ltvItem = .Items.Add(datei.Name, SHI.iIcon)
'ltvItem.SubItems.Add(CStr((CalculateSize(datei.Length))))
ltvItem.SubItems.Add(datei.LastWriteTime)
ltvItem.SubItems.Add(datei.FullName)
ltvItem.ToolTipText = datei.FullName
ltvItem.Tag = datei
Next
.EndUpdate()
End With
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler")
End Try
End Sub Und hier das Module
Private Const MAX_PATH = 260
'***************************************************************************
' ********
Public Declare Function SHGetFileInfo Lib "shell32.dll" Alias _
"SHGetFileInfoA" _
(ByVal pszPath As String, ByVal dwFileAttributes As Integer, ByRef psfi As _
SHFILEINFO, _
ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer
'Structure for shGetFileInfo.
Public Structure SHFILEINFO
Public hIcon As Integer
Public iIcon As Integer .
Public dwAttributes As Integer
<VBFixedString(MAX_PATH), System.Runtime.InteropServices.MarshalAs( _
System.Runtime.InteropServices.UnmanagedType.ByValTStr, _
SizeConst:=MAX_PATH)> Public szDisplayName As String
<VBFixedString(80), System.Runtime.InteropServices.MarshalAs( _
System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeName As String
'String that describes the type of file.
End Structure
'Flags that specify the file information to retrieve.
'For more information on these flags, look up SHGetFileInfo on the Microsoft
' MSDN.
Public Enum SHGFI
ATTRIBUTES = &H800&
DISPLAYNAME = &H200
EXETYPE = &H2000S
ICON = &H100
ICONLOCATION = &H1000&
LARGEICON = &H0
LINKOVERLAY = &H8000&
OPENICON = &H2
PIDL = &H8&
SELECTED = &H10000
SHELLICONSIZE = &H4S
SMALLICON = &H1
SYSICONINDEX = &H4000
TYPENAME = &H400
USEFILEATTRIBUTES = &H10
End Enum
'***************************************************************************
' ********
Declare Function DestroyIcon Lib "user32.dll" (ByVal hIcon As Integer) As _
Integer
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _
hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal _
lParam As Integer) As Integer
'ListView Constants.
Public Const LVM_FIRST = &H1000&
Public Const LVM_SETIMAGELIST = (LVM_FIRST + 3)
'***************************************************************************
' ******** Wäre schön, wenn jemand hier meinem Fehler entdecken könnte
Gruß
Lars
P.S: Wenn jemand noch ne andere Möglichkeit kennt, Verzeichnisse mit einem Icon in einem Listview einzulesen, immer her damit 
Visual Basic Express 2010, C# Express 2010
Intel? i7 Core 920@2.63 Ghz, 6 GByte DDR3, 1024 MB ATI? Radeon? 5870, Windows 7 Prof. 64 Bit
Gibson SG 61' Reissue ; Ibanez Prestige RG 2550 E, Marshall Valvestate VS100 + Modell Vs412 (4 x 12''), Cubase Studio |