Announcement

Collapse
No announcement yet.

ListView, Verzeichnisse und Icons

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • ListView, Verzeichnisse und Icons

    Hi,

    ich möchte in einem ListView ein Verzeichnis anzeigen, wobei ich leider auf ein paar Probleme stoße ...

    Lese ich nur Dateien aus, so klappt das mit der Zuweisung der Icons.
    Kommen die Verzeichnisse dazu, so gibt es nur noch Chaos.
    Weiß jemand, wie man an die Icons der Verzeichnisse kommt?

    Auch finde ich nicht herraus, wie man an die Dateizuweisung einer Datei kommt.
    Habe zwar die Funktion GetFileDescription im Internet gefunden, aber diese funktioniert nicht.

    Schöner wäre das ganze ja über 'GetFileSystemEntries' - aber dann sind Verzeichnisse und Dateien gemischt, oder?

    Code:
    Imports System.IO
    Imports System.Runtime.InteropServices
    Public Class Form1
    
        Private Const SHGFI_TYPENAME As Int32 = &H400
    
        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 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
    
        Private Const SHGFI_ICON = &H100
        Private Const SHGFI_SMALLICON = &H1
        Private Const SHGFI_LARGEICON = &H0    ' Large icon
        Private Const MAX_PATH = 260
    
        Private Sub AddImages(ByVal strFileName As String)
    
            Dim shInfo As New SHFILEINFO
    
            shInfo.szDisplayName = New String(vbNullChar, MAX_PATH)
            shInfo.szTypeName = New String(vbNullChar, 80)
            Dim hIcon As IntPtr
            hIcon = SHGetFileInfo(strFileName, 0, shInfo, Marshal.SizeOf(shInfo), SHGFI_ICON Or SHGFI_SMALLICON)
            Dim MyIcon As Drawing.Bitmap
            MyIcon = Drawing.Icon.FromHandle(shInfo.hIcon).ToBitmap
            ImageList1.Images.Add(strFileName.ToString(), MyIcon)
            System.Console.WriteLine(strFileName.ToString())
        End Sub
    
        Private Function GetFileDescription(ByVal FileName As String) As String
            Dim shfi As New SHFILEINFO
            SHGetFileInfo(FileName, 0, shfi, Marshal.SizeOf(shfi), SHGFI_TYPENAME)
            Return shfi.szTypeName
        End Function
    
        Private Sub GetFolders(ByVal FolderPath As String)
            Dim SubItemIndex As Integer
            ListView1.Items.Clear()
            Try
                For Each FolderNode As String In Directory.GetDirectories(FolderPath)
                    ListView1.Items.Add(FolderNode.Substring(FolderNode.LastIndexOf("\"c) + 1), FolderNode.ToString)
                    ListView1.Items(SubItemIndex).SubItems.Add("")
                    ListView1.Items(SubItemIndex).SubItems.Add("")
                    AddImages(FolderNode)
                    SubItemIndex += 1
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
    
        End Sub
    
        Private Sub GetFiles(folder as String)
            Dim FileExtension As String
            Dim SubItemIndex As Integer
            Dim DateMod As String
    
            Try
                For Each file As String In IO.Directory.GetFiles(folder)
                    FileExtension = IO.Path.GetExtension(file)
                    DateMod = IO.File.GetLastWriteTime(file).ToString()
    
                    AddImages(file)
                    ListView1.Items.Add(file.Substring(file.LastIndexOf("\"c) + 1), file.ToString())
                    'ListView1.Items(SubItemIndex).SubItems.Add(GetFileDescription(file))
                    ListView1.Items(SubItemIndex).SubItems.Add(FileExtension.ToString)
                    ListView1.Items(SubItemIndex).SubItems.Add(DateMod.ToString())
                    SubItemIndex += 1
    
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            ListView1.View = View.Details
            ' Add a column with width 80 and left alignment
            ListView1.Columns.Add("File Name", 150, HorizontalAlignment.Left)
            ListView1.Columns.Add("File Type", 80, HorizontalAlignment.Left)
            ListView1.Columns.Add("Date Modified", 150, HorizontalAlignment.Left)
    
            GetFolders("c:\programme")
        End Sub
    
        Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
            TextBox1.Text = ListView1.SelectedItems(0).SubItems(0).Text
        End Sub
    End Class
    Danke,

    Andre
Working...
X