Vb.net 文件图标和列表视图

Vb.net 文件图标和列表视图,vb.net,Vb.net,如何检索与文件类型关联的文件图标,并将其添加到vb.net中的Listview项中 我读过关于SHGetFileInfo的文章,但我对此一无所知 请给出解决方案或向我详细解释系统如何使用.net控件查阅后,我可以看出您对此感到困惑的原因,但我认为这可能有点过分,因为我认为您正在尝试执行的操作,即枚举文件夹的内容并将项添加到Listview 如果我们有一个表单,其中包含一个ListView和一个ImageList,并且通过将ListView的LargeImageList属性设置为ImageList

如何检索与文件类型关联的文件图标,并将其添加到vb.net中的Listview项中

我读过关于SHGetFileInfo的文章,但我对此一无所知

请给出解决方案或向我详细解释系统如何使用.net控件查阅后,我可以看出您对此感到困惑的原因,但我认为这可能有点过分,因为我认为您正在尝试执行的操作,即枚举文件夹的内容并将项添加到Listview

如果我们有一个表单,其中包含一个ListView和一个ImageList,并且通过将ListView的LargeImageList属性设置为ImageList来关联这两个表单,那么下面是我们如何将文件夹的内容放入ListView中,每个文件的图标来自相关的EXE文件

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's image
            ListView1.Items.Add(fileInfo.Name, exePath)
        End If
    Next

End Sub
GetAssociatedProgram来自

公共函数GetAssociatedProgram(ByVal文件扩展名为_
字符串)作为字符串
'返回与指定对象关联的应用程序
'文件扩展名
'ie,“VB”文件的路径\denenv.exe
Dim objExtReg作为Microsoft.Win32.RegistryKey=_
Microsoft.Win32.Registry.ClassesRoot
Dim objAppReg作为Microsoft.Win32.RegistryKey=_
Microsoft.Win32.Registry.ClassesRoot
将strExtValue设置为字符串
尝试
'如果不存在尾随句点,则添加尾随句点
如果FileExtension.Substring(0,1)“.”,则_
FileExtension=“.”和FileExtension
'打开包含启动应用程序详细信息的注册表区域
objExtReg=objExtReg.OpenSubKey(FileExtension.Trim)
strExtValue=objExtReg.GetValue(“”).ToString
objapreg=objapreg.OpenSubKey(strExtValue&_
“\shell\open\command”)
'解析、整理并返回结果
Dim SplitArray()作为字符串
SplitArray=Split(objAppReg.GetValue(Nothing.ToString,“”)
如果SplitArray(0).Trim.Length>0,则
返回SplitArray(0)。替换(“%1”和“”)
其他的
返回SplitArray(1)。替换(“%1”和“”)
如果结束
抓住
返回“”
结束尝试
端函数
最后,当您在此文件夹上运行此代码时: 您应该得到:

如果您知道所需图标的文件的文件名,则可以使用该功能。e、 g

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

您也可以参考我对a的回答以了解更多的实现细节。

EXECONG=Drawing.Icon.ExtractAssociatedIcon(exePath)对于此语句,它给我的路径异常不合法。如果文件夹中的某个文件没有扩展名,我可以获得该异常。在这种情况下,您需要排除没有扩展名的文件(我编辑了答案以显示此内容),或者您需要在ImageList中添加一个空白文件图标,然后对任何没有扩展名的文件使用该图标。可能存在重复的
Icon ico = System.Drawing.Icon.ExtractAssociatedIcon(@"C:\WINDOWS\system32\notepad.exe");