小编典典

获取Shell使用的文件图标

c#

在.Net(C#或VB:无关)中,给定文件路径字符串,FileInfo结构或FileSystemInfo结构用于实际的现有文件,我如何确定外壳程序(浏览器)为此使用的图标文件?

我目前不打算将其用于任何用途,但是当看到这个问题时,我对如何做到这一点感到好奇,并且我认为将其存档在SO上将很有用。


阅读 248

收藏
2020-05-19

共1个答案

小编典典

Imports System.Drawing
Module Module1

    Sub Main()    
        Dim filePath As String =  "C:\myfile.exe"  
        Dim TheIcon As Icon = IconFromFilePath(filePath)

        If TheIcon IsNot Nothing Then    
            ''#Save it to disk, or do whatever you want with it.
            Using stream As New System.IO.FileStream("c:\myfile.ico", IO.FileMode.CreateNew)
                TheIcon.Save(stream)          
            End Using
        End If
    End Sub

    Public Function IconFromFilePath(filePath As String) As Icon
        Dim result As Icon = Nothing
        Try
            result = Icon.ExtractAssociatedIcon(filePath)
        Catch ''# swallow and return nothing. You could supply a default Icon here as well
        End Try
        Return result
    End Function
End Module
2020-05-19