文件图标和列表视图

shr*_*dha 4 vb.net

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

我读到了SHGetFileInfo,但我对此一无所知

请给我解决方案或请解释我的系统与.net控件的详细工作

Phi*_*ove 6

说完抬头的SHGetFileInfo我明白为什么你被它迷惑,但我认为这可能是什么,我认为你正在试图做的就是枚举文件夹的内容,并添加项目到ListView略有矫枉过正.

如果我们有一个包含一个ListView和一个ImageList,首先与由ListView的LargeImageList属性设置到ImageList相关的两个表格,然后在这里是我们如何把一个文件夹的内容与图标从相关的EXE文件来ListView控件对于每个文件.

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
Run Code Online (Sandbox Code Playgroud)

GetAssociatedProgram来自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
Run Code Online (Sandbox Code Playgroud)

在这一切结束时,当你运行该文件夹下面的代码: 替代文字http://www.philippursglove.com/stackoverflow/listview1.png 你应该得到的:
替代文字http://www.philippursglove.com/计算器/ listview2.png