gpt4 book ai didi

vb.net - 如何获取文件的文件类型

转载 作者:行者123 更新时间:2023-12-02 03:54:45 24 4
gpt4 key购买 nike

有没有办法让VB.net中的Windows资源管理器中显示的文件类型

例如,在 Windows 资源管理器中的详细信息 View 中可以看到

Name          Date Modified           Type                           SizeA.PDF         05/06/2017 5:54PM       Adobe Acrobat reader           150kbB.DOCX        05/06/2017 5:00PM       Microsoft Word Document        100kbetc.

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 获取修改后的数据和文件大小......但不是类型。

感谢论坛的帮助!

。 IE。对于 .pdf 文件,显示为“adobe acrobat 文档”。.xls 文件将显示“Microsoft Excel 工作表”

最佳答案

您可以使用下面的 VB.net 代码来获取文件类型描述。基本上,您必须使用 SHGetFileInfo API 来获取该信息。

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

关于vb.net - 如何获取文件的文件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44371042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com