习惯于通过shell文件图标 [英] Get File Icon used by Shell

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

问题描述

在。NET(C#或VB:不在乎),给定一个文件路径字符串,FileInfo的结构或FileSystemInfo结构为一个真正存在的文件,我怎么能确定由外壳的图标(S)(探险家),该文件?

我并不是目前正计划使用任何东西,但是当在看<一我开始好奇怎么办呢href="http://stackoverflow.com/questions/462232/what-is-the-best-vb-net-control-standard-custom-for-displaying-list-of-files">this问题,我认为这将是在这里存档的那么有用。

解决方案

 进口System.Drawing中
模块模块1

    副主()
        昏暗文件路径作为字符串=C:\ myfile.exe
        昏暗TheIcon为图标= IconFromFilePath(文件路径)

        如果TheIcon状态并没有任何然后
            ''#Save到磁盘,或做任何你想做的事情。
            使用流作为新System.IO.FileStream(C:\ myfile.ico,IO.FileMode.CreateNew)
                TheIcon.Save(流)
            结束使用
        结束如果
    结束小组

    公共功能IconFromFilePath(文件路径作为字符串)作为图标
        昏暗的结果为图标=无
        尝试
            结果= Icon.ExtractAssociatedIcon(文件路径)
        抓''#燕子和返回任何结果。你可以提供一个默认的图标在这里也
        结束尝试
        返回结果
    端功能
前端模块
 

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?

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天全站免登陆