如何使用 VB.net 获取 Windows 文件的详细信息?
我所说的详细信息类型是指当我右键单击某个文件(例如 MS Word 文档),然后单击“属性”并选择“详细信息”选项卡时找到的详细信息类型。
我知道有些可以通过 FileInfo 获取,但不是全部,例如“Tags”。 谢谢
最佳答案
对于这些东西,你需要使用 Shell32。从 COM 选项卡中,找到并添加 Microsoft Shell 控件和自动化。以下是为给定文件创建属性值列表的代码:
' class to hold the goodies
Friend Class ShellInfo
Public Property Name As String
Public Property Value As String
Public Sub New(n As String, v As String)
Name = n
Value = v
End Sub
Public Overrides Function ToString() As String
Return Name
End Function
End Class
然后用一个函数来填充它
Private Function GetXtdShellInfo(filepath As String) As List(Of ShellInfo)
' ToDo: add error checking, maybe Try/Catch and
' surely check if the file exists before trying
Dim xtd As New List(Of ShellInfo)
Dim shell As New Shell32.Shell
Dim shFolder As Shell32.Folder
shFolder = shell.NameSpace(Path.GetDirectoryName(filepath))
' its com so iterate to find what we want -
' or modify to return a dictionary of lists for all the items
Dim key As String
For Each s In shFolder.Items
' look for the one we are after
If shfolder.GetDetailsOf(s, 0).ToLowerInvariant = Path.GetFileName(file).ToLowerInvariant Then
Dim ndx As Int32 = 0
key = shfolder.GetDetailsOf(shfolder.Items, ndx)
' there are a varying number of entries depending on the OS
' 34 min, W7=290, W8=309 with some blanks
' this should get up to 310 non blank elements
Do Until String.IsNullOrEmpty(key) AndAlso ndx > 310
If String.IsNullOrEmpty(key) = False Then
xtd.Add(New ShellInfo(key,
shfolder.GetDetailsOf(s, ndx)))
End If
ndx += 1
key = shfolder.GetDetailsOf(shfolder.Items, ndx)
Loop
' we got what we came for
Exit For
End If
Next
Return xtd
End Function
使用很简单:
Dim xtd As List(Of ShellInfo) = GetXtdShellInfo("C:\Temp\Capri.jpg")
For Each s As ShellInfo In xtd
Console.WriteLine("{0}: {1}", s.Name, s.Value)
Next
返回应该是 ShellInfo
项的列表,其中 Name 是属性名称,例如 Name、BitRate、Album
和关联的 Value
将由 Shell32
返回。例如
Name: Capri.jpg
Size: 15.2 KB
Item type: Image File
Date modified: 7/20/2014 12:19 PM
Date created: 7/20/2014 12:17 PM
Date accessed: 7/20/2014 12:17 PM
(etc)
实际返回的数量将根据操作系统版本而有所不同
正如评论中所述,Microsoft Shell 控制和自动化已重命名为 Microsoft Shell 文件夹 View 路由器(在 Windows 8.1 中)。
此外,前 35 个属性相当知名且更常见,但在 Win7 中大约有 291 个。在 Windows 8 下,最大值为 309,有一些空白点,并且在列表深处,一些属性索引发生了更改。
查看此答案相关问题How to read the bit rate information from a .mov video file header
关于vb.net - 获取扩展文件信息详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26142723/