问题描述
有没有办法在VB.net的Windows资源管理器中显示文件类型
Is there a way to get the file type displayed in windows explorer in VB.net
例如在Windows资源管理器的详细信息视图中,例如可以看到
i.e in windows explorer in details view one can see for example
Name Date Modified Type Size
A.PDF 05/06/2017 5:54PM Adobe Acrobat reader 150kb
B.DOCX 05/06/2017 5:00PM Microsoft Word Document 100kb
etc.
我想获取类型.我似乎找不到办法到达那里.感觉这应该很容易.
I want to get the type. I cant seem to find a way to get there. It feels like this should be very easy.
Dim infoReader As System.IO.FileInfo
infoReader = My.Computer.FileSystem.GetFileInfo(txtFileName.Text)
FileInfo让我修改了文件的数据和大小.但是没有类型.
FileInfo gets me modified data and size of the file.. but not the type.
感谢论坛的帮助!
. IE.对于.pdf文件,显示为"adobe acrobat文档"..xls文件为"Microsoft excel工作表"
. I.e. for .pdf file the display with would "adobe acrobat document" .xls file would be "Microsoft excel worksheet"
推荐答案
您可以使用下面的VB.net代码获取文件类型描述.基本上,您必须使用 SHGetFileInfo API来获取该信息.
You can use below VB.net code for getting the file type description. Basically you have to use SHGetFileInfo API to get that information.
Imports System.Runtime.InteropServices
Module Get_File_Type
Sub Main()
Dim info As New NativeMethods.SHFILEINFO()
Dim fileName As String = "C:\TEST\TEST.xlsx"
Dim dwFileAttributes As UInteger = NativeMethods.FILE_ATTRIBUTE.FILE_ATTRIBUTE_NORMAL
Dim uFlags As UInteger = CUInt(NativeMethods.SHGFI.SHGFI_TYPENAME Or NativeMethods.SHGFI.SHGFI_USEFILEATTRIBUTES)
NativeMethods.SHGetFileInfo(fileName, dwFileAttributes, info, CUInt(Marshal.SizeOf(info)), uFlags)
Console.WriteLine(info.szTypeName)
Console.ReadLine()
End Sub
End Module
NotInheritable Class NativeMethods
Private Sub New()
End Sub
<StructLayout(LayoutKind.Sequential)> _
Public Structure SHFILEINFO
Public hIcon As IntPtr
Public iIcon As Integer
Public dwAttributes As UInteger
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeName As String
End Structure
Public NotInheritable Class FILE_ATTRIBUTE
Private Sub New()
End Sub
Public Const FILE_ATTRIBUTE_NORMAL As UInteger = &H80
End Class
Public NotInheritable Class SHGFI
Private Sub New()
End Sub
Public Const SHGFI_TYPENAME As UInteger = &H400
Public Const SHGFI_USEFILEATTRIBUTES As UInteger = &H10
End Class
<DllImport("shell32.dll")> _
Public Shared Function SHGetFileInfo(pszPath As String, dwFileAttributes As UInteger, ByRef psfi As SHFILEINFO, cbSizeFileInfo As UInteger, uFlags As UInteger) As IntPtr
End Function
End Class
这篇关于如何获取文件的文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!