HOW TO:获取文件类型图标和类型说明

本文介绍了一种使用 C# 编程语言通过调用 Shell32.dll 中的 SHGetFileInfo 函数来获取指定文件的图标、文件名和类型的方法。此外,还提供了完整的示例代码,包括一个用户界面,用于选择文件并显示其图标和相关信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Author:水如烟  

如果查看一个有效文件,信息如下:

如果查看文件类型,比如输入“.doc”,信息如下:


类:

Imports  System.Runtime.InteropServices

Namespace  LzmTW.uSystem.uWindow.WinAPI32

    
Friend   Class  UnSafeNativeMethods

        
< Flags() >  _
        
Public   Enum  SHGFI
            SHGFI_ICON 
=   & H100               '  get icon
            SHGFI_DISPLAYNAME  =   & H200        '  get display name
            SHGFI_TYPENAME  =   & H400           '  get type name
            SHGFI_ATTRIBUTES  =   & H800         '  get attributes
            SHGFI_ICONLOCATION  =   & H1000      '  get icon location
            SHGFI_EXETYPE  =   & H2000           '  return exe type
            SHGFI_SYSICONINDEX  =   & H4000      '  get system icon index
            SHGFI_LINKOVERLAY  =   & H8000       '  put a link overlay on icon
            SHGFI_SELECTED  =   & H10000         '  show icon in selected state
            SHGFI_ATTR_SPECIFIED  =   & H20000   '  get only specified attributes
            SHGFI_LARGEICON  =   & H0            '  get large icon
            SHGFI_SMALLICON  =   & H1            '  get small icon
            SHGFI_OPENICON  =   & H2             '  get open icon
            SHGFI_SHELLICONSIZE  =   & H4        '  get shell size icon
            SHGFI_PIDL  =   & H8                 '  pszPath is a pidl
            SHGFI_USEFILEATTRIBUTES  =   & H10   '  use passed dwFileAttribute
         End Enum

        
< Flags() >  _
        
Public   Enum  FILE_ATTRIBUTE
            FILE_ATTRIBUTE_READONLY 
=   & H1
            FILE_ATTRIBUTE_HIDDEN 
=   & H2
            FILE_ATTRIBUTE_SYSTEM 
=   & H4
            FILE_ATTRIBUTE_DIRECTORY 
=   & H10
            FILE_ATTRIBUTE_ARCHIVE 
=   & H20
            FILE_ATTRIBUTE_DEVICE 
=   & H40
            FILE_ATTRIBUTE_NORMAL 
=   & H80
            FILE_ATTRIBUTE_TEMPORARY 
=   & H100
            FILE_ATTRIBUTE_SPARSE_FILE 
=   & H200
            FILE_ATTRIBUTE_REPARSE_POINT 
=   & H400
            FILE_ATTRIBUTE_COMPRESSED 
=   & H800
            FILE_ATTRIBUTE_OFFLINE 
=   & H1000
            FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 
=   & H2000
            FILE_ATTRIBUTE_ENCRYPTED 
=   & H4000
        
End Enum

        
< StructLayout(LayoutKind.Sequential) >  _
        
Public   Structure  SHFILEINFO

            
Public  hIcon  As  IntPtr
            
Public  iIcon  As   Integer
            
Public  dwAttributes  As   Integer

            
< MarshalAs(UnmanagedType.ByValTStr, SizeConst: = 260 ) >  _
            
Public  szDisplayName  As   String
            
< MarshalAs(UnmanagedType.ByValTStr, SizeConst: = 80 ) >  _
            
Public  szTypeName  As   String
        
End Structure

        
< DllImport( " Shell32.dll " ) >  _
        
Public   Shared   Function  SHGetFileInfo( _
            
ByVal  pszPath  As   String , _
            
ByVal  dwFileAttributes  As  FILE_ATTRIBUTE, _
            
< Out() >   ByRef  psfi  As  SHFILEINFO, _
            
ByVal  cbfileInfo  As   Integer , _
            
ByVal  uFlags  As  SHGFI _
        ) 
As  IntPtr
        
End Function


    
End Class

End Namespace

 

Imports  System.Runtime.InteropServices

Namespace  LzmTW.uSystem.uDrawing

    
Public   Class  FileType
        
Private  gIcon  As  Icon
        
Private  gDeclare  As   String
        
Private  gType  As   String

        
Sub   New ( ByVal  file  As   String )

            
Dim  flags  As  uWindow.WinAPI32.UnSafeNativeMethods.SHGFI

            
Dim  info  As   New  uWindow.WinAPI32.UnSafeNativeMethods.SHFILEINFO
            
Dim  size  As   Integer   =  Marshal.SizeOf(info)

            
Dim  attr  As  uWindow.WinAPI32.UnSafeNativeMethods.FILE_ATTRIBUTE

            attr 
=  uWindow.WinAPI32.UnSafeNativeMethods.FILE_ATTRIBUTE.FILE_ATTRIBUTE_NORMAL

            flags 
=  uWindow.WinAPI32.UnSafeNativeMethods.SHGFI.SHGFI_DISPLAYNAME  Or  _
                uWindow.WinAPI32.UnSafeNativeMethods.SHGFI.SHGFI_TYPENAME 
Or  _
                uWindow.WinAPI32.UnSafeNativeMethods.SHGFI.SHGFI_ICON 
Or  _
                uWindow.WinAPI32.UnSafeNativeMethods.SHGFI.SHGFI_USEFILEATTRIBUTES

            uWindow.WinAPI32.UnSafeNativeMethods.SHGetFileInfo(file, attr, info, size, flags)

            
With  info
                gIcon 
=  Drawing.Icon.FromHandle(.hIcon)
                gDeclare 
=  .szDisplayName
                gType 
=  .szTypeName
            
End   With

        
End Sub

        
Public   ReadOnly   Property  Icon()  As  Icon
            
Get
                
Return  gIcon
            
End   Get
        
End Property

        
Public   ReadOnly   Property  [ Declare ]()  As   String
            
Get
                
Return  gDeclare
            
End   Get
        
End Property

        
Public   ReadOnly   Property  [Type]()  As   String
            
Get
                
Return  gType
            
End   Get
        
End Property

    
End Class

End Namespace

界面:

Public   Class  Form1

    
Private   Sub  Button1_Click_1( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button1.Click
        
Using  dlg  As   New  OpenFileDialog
            
With  dlg
                .Filter 
=   " 所有文件(*.*)|*.* "
                .Title 
=   " 选择文件查看文件类型信息 "
                
If   Not   String .IsNullOrEmpty( Me .TextBox1.Text)  Then
                    
With  FileIO.FileSystem.GetDirectoryInfo( Me .TextBox1.Text)
                        
If  .Exists  Then
                            dlg.InitialDirectory 
=  .FullName
                        
End   If
                    
End   With
                
End   If

                .Multiselect 
=   False

                
If  .ShowDialog  =  Windows.Forms.DialogResult.OK  Then
                    
Me .TextBox1.Text  =  .FileName
                
End   If
            
End   With

        
End   Using
    
End Sub

    
Private   Sub  Button2_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button2.Click
        
Dim  filetype  As   New  LzmTW.uSystem.uDrawing.FileType( Me .TextBox1.Text)
        
Me .PictureBox1.Image  =  filetype.Icon.ToBitmap
        
Me .Label2.Text  =  GetDeclareInfo(filetype)
    
End Sub

    
Private   Function  GetDeclareInfo( ByVal  info  As  LzmTW.uSystem.uDrawing.FileType)  As   String
        
Dim  b  As   New  System.Text.StringBuilder
        b.AppendLine(
" 详细信息 " )
        b.AppendLine()
        b.AppendLine(info.Declare)
        b.AppendLine(info.Type)

        
If   Not   String .IsNullOrEmpty( Me .TextBox1.Text)  Then
            
With  FileIO.FileSystem.GetFileInfo( Me .TextBox1.Text)
                
If  .Exists  Then
                    b.AppendLine(
String .Format( " 修改日期:{0} " , .LastWriteTime))
                    b.AppendLine(
String .Format( " 大小:{0}字节 " , .Length))
                
End   If
            
End   With
        
End   If

        
Return  b.ToString
    
End Function
End Class

 

代码下载:代码

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值