none
Get desktop icons RRS feed

  • Question

  • Good ...

    is there any way to get the desktop icons (SysListView32) in windows 7 and show in ListView?

    I hope you can help me ...

    Thanks 

    Monday, November 12, 2018 4:39 PM

All replies

  • One of the ways is to use Shell.NameSpace

    and enumerate child items (FolderItem)

    Monday, November 12, 2018 9:35 PM
  • Hello,

    Hope below code helps you

    Imports System.IO
    
    Public Class Form15
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Try
                Dim ImageList1 As New ImageList
                ListView1.LargeImageList = ImageList1
                ListView1.SmallImageList = ImageList1
                ListView1.View = View.LargeIcon
    
                Dim dirInfo As DirectoryInfo
                Dim fileInfo As FileInfo
                Dim exeIcon As System.Drawing.Icon
    
                dirInfo = New DirectoryInfo("C:\Users\Admin\Desktop\")
    
                For Each fileInfo In dirInfo.GetFiles
                    If (Not String.IsNullOrEmpty(fileInfo.Extension)) Then
    
                        exeIcon = System.Drawing.Icon.ExtractAssociatedIcon(fileInfo.FullName)
    
                        If (ImageList1.Images.ContainsKey(fileInfo.FullName)) Then
                            ListView1.Items.Add(fileInfo.Name, fileInfo.FullName)
                        ElseIf (Not exeIcon Is Nothing) Then
                            ImageList1.Images.Add(fileInfo.FullName, exeIcon)
                            ListView1.Items.Add(fileInfo.Name, fileInfo.FullName)
                        Else
                            ListView1.Items.Add(fileInfo.Name)
                        End If
    
                    End If
                Next
            Catch ex As Exception
                Dim errormsg As String = String.Empty
                errormsg = ex.ToString()
            End Try
        End Sub
    End Class

    Tuesday, November 13, 2018 6:20 AM
  • Hi,

    try the code,

     Public Class ExtractIcon
            <DllImport("Shell32.dll")>
            Private Shared Function SHGetFileInfo(ByVal pszPath As String, ByVal dwFileAttributes As UInteger, <Out> ByRef psfi As SHFILEINFO, ByVal cbfileInfo As UInteger, ByVal uFlags As SHGFI) As Integer
    
            End Function
    
            <StructLayout(LayoutKind.Sequential)>
            Private Structure SHFILEINFO
                Public Sub New(ByVal b As Boolean)
                    hIcon = IntPtr.Zero
                    iIcon = 0
                    dwAttributes = 0
                    szDisplayName = ""
                    szTypeName = ""
                End Sub
    
                Public hIcon As IntPtr
                Public iIcon As Integer
                Public dwAttributes As UInteger
                <MarshalAs(UnmanagedType.LPStr, SizeConst:=260)>
                Public szDisplayName As String
                <MarshalAs(UnmanagedType.LPStr, SizeConst:=80)>
                Public szTypeName As String
            End Structure
    
            Private Enum SHGFI
                SmallIcon = &H1
                LargeIcon = &H0
                Icon = &H100
                DisplayName = &H200
                Typename = &H400
                SysIconIndex = &H4000
                UseFileAttributes = &H10
            End Enum
    
            Public Shared Function GetIcon(ByVal strPath As String, ByVal bSmall As Boolean) As Icon
                Dim info As SHFILEINFO = New SHFILEINFO(True)
                Dim cbFileInfo As Integer = Marshal.SizeOf(info)
                Dim flags As SHGFI
    
                If bSmall Then
                    flags = SHGFI.Icon Or SHGFI.SmallIcon Or SHGFI.UseFileAttributes
                Else
                    flags = SHGFI.Icon Or SHGFI.LargeIcon Or SHGFI.UseFileAttributes
                End If
    
                SHGetFileInfo(strPath, 256, info, CUInt(cbFileInfo), flags)
                Return Icon.FromHandle(info.hIcon)
            End Function
        End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 13, 2018 7:36 AM
  • Thank you
    Tuesday, November 13, 2018 4:09 PM
  • Thank you
    Tuesday, November 13, 2018 4:10 PM
  •  Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. 
    Tuesday, November 13, 2018 5:01 PM