获取 Shell 使用的文件图标 [英] Get File Icon used by Shell

查看:30
本文介绍了获取 Shell 使用的文件图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 .Net(C# 或 VB:不关心)中,给定文件路径字符串、FileInfo 结构或 FileSystemInfo 结构的真实现有文件,我如何确定外壳使用的图标(资源管理器)) 对于那个文件?

In .Net (C# or VB: don't care), given a file path string, FileInfo struct, or FileSystemInfo struct for a real existing file, how can I determine the icon(s) used by the shell (explorer) for that file?

我目前不打算将它用于任何用途,但是在查看 这个问题,我认为在 SO 上存档会很有用.

I'm not currently planning to use this for anything, but I became curious about how to do it when looking at this question and I thought it would be useful to have archived here on SO.

推荐答案

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

这篇关于获取 Shell 使用的文件图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆