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
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
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
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
代码下载:代码