Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to make a program similar to file explorer so i am writing code to show drive letters with their drive icon in a list view box but i don't know how to put their icon in the list view box.
I am using Microsoft visual basic 2010 express.

What I have tried:

Imports System
Imports System.IO

Public Class Test


    Private Sub Test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim drives As String() = System.IO.Directory.GetLogicalDrives()
        For Each drive As String In drives
            ListView1.Items.Add(drive)
            'I need to add the icon of the drive
        Next

    End Sub
End Class
Posted
Updated 21-Jan-18 4:49am
v3

There is only one identical icon for local drives. It can Drive icons can be retrieved using the Icon.ExtractAssociatedIcon Method (String) (System.Drawing)[^] by passing a drive string without path like "C:".

To add icons to a list view, you need to create an ImageList, add the icons to that list, and set the corresponding key that has been used to add the icon to the image list when adding the list view item. A C# example can be found at How to: Extract the Icon Associated with a File in Windows Forms | Microsoft Docs[^] (old version with VB.Net code: Gewusst wie: Extrahieren eines mit einer Datei verknüpften Symbols in Windows Forms[^]; ignore the German title, the content is English).
 
Share this answer
 
v2
Comments
Member 13607190 19-Jan-18 8:57am    
" There is only one identical icon for local drives. "
No The drive icon varies for C drive
Jochen Arndt 19-Jan-18 9:18am    
You are right.
There are different icons for different hardware types (HDD, CD).
Don't just download and copy paste the code.First run the sample check the output then debug and see what is happening in the source code then try to implement similarly with your approach.

Implementing a common File system browser with a ListView[^]
 
Share this answer
 
Comments
Member 13607190 19-Jan-18 8:40am    
I don't know how to add image of drive in image list. That is what i am asking.
[no name] 19-Jan-18 8:43am    
What??You can download PNG's of drives and add them to the project folder.
Member 13607190 19-Jan-18 8:52am    
I dont want to use my png file i need to get the exact system drive icon
[no name] 19-Jan-18 8:57am    
Please check the link.https://stackoverflow.com/questions/12840268/how-to-get-the-drive-icons-as-used-in-windows-explorer
Member 13607190 19-Jan-18 9:19am    
It is in c#

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900