File Icons and List View

How to get file icons associated with file types and add them with Listview items in vb.net

I read about SHGetFileInfo, but I didn't understand anything from this

please give me a solution or please explain to me that the ho system works with .net controls.

+4
source share
2 answers

By looking at SHGetFileInfo I understand why you are confused about this, but I think it can be a little too much for what I think you are trying to do, which enumerates the contents of the folder and adds items to the Listview.

If we have a form that contains a ListView and an ImageList, with two associated with the ListView property LargeImageList set in the ImageList, then this is how we put the contents of a folder into a ListView with the icons coming from the associated EXE file for each file.

Imports System.IO Imports System.Drawing Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim dirInfo As DirectoryInfo Dim fileInfo As FileInfo Dim exePath As String Dim exeIcon As Icon dirInfo = New DirectoryInfo(path_to_some_folder 'We use this For...Each to iterate over the collection of files in the folder For Each fileInfo In dirInfo.GetFiles 'We can only find associated exes by extension, so don't show any files that have no extension If fileInfo.Extension = String.Empty Then Else 'Use the function to get the path to the executable for the file exePath = GetAssociatedProgram(fileInfo.Extension) 'Use ExtractAssociatedIcon to get an icon from the path exeIcon = Drawing.Icon.ExtractAssociatedIcon(exePath) 'Add the icon if we haven't got it already, with the executable path as the key If ImageList1.Images.ContainsKey(exePath) Then Else ImageList1.Images.Add(exePath, exeIcon) End If 'Add the file to the ListView, with the executable path as the key to the ImageList image ListView1.Items.Add(fileInfo.Name, exePath) End If Next End Sub 

GetAssociatedProgram comes from developer.com

 Public Function GetAssociatedProgram(ByVal FileExtension As _ String) As String ' Returns the application associated with the specified ' FileExtension ' ie, path\denenv.exe for "VB" files Dim objExtReg As Microsoft.Win32.RegistryKey = _ Microsoft.Win32.Registry.ClassesRoot Dim objAppReg As Microsoft.Win32.RegistryKey = _ Microsoft.Win32.Registry.ClassesRoot Dim strExtValue As String Try ' Add trailing period if doesn't exist If FileExtension.Substring(0, 1) <> "." Then _ FileExtension = "." & FileExtension ' Open registry areas containing launching app details objExtReg = objExtReg.OpenSubKey(FileExtension.Trim) strExtValue = objExtReg.GetValue("").ToString objAppReg = objAppReg.OpenSubKey(strExtValue & _ "\shell\open\command") ' Parse out, tidy up and return result Dim SplitArray() As String SplitArray = Split(objAppReg.GetValue(Nothing).ToString, """") If SplitArray(0).Trim.Length > 0 Then Return SplitArray(0).Replace("%1", "") Else Return SplitArray(1).Replace("%1", "") End If Catch Return "" End Try End Function 

At the end of all this, when you run this code in this folder: alt text http://www.philippursglove.com/stackoverflow/listview1.png you should get:
alt text http://www.philippursglove.com/stackoverflow/listview2.png

+6
source

If you know the file name of the file whose icon you want, you can use System.Drawing.Icon.ExtractAssociatedIcon for this purpose. eg.

 Icon ico = System.Drawing.Icon.ExtractAssociatedIcon(@"C:\WINDOWS\system32\notepad.exe"); 

You can also refer to my answer to the question for more information about the implementation.

-one
source

All Articles