获取扩展文件信息详细信息

Tob*_*oby 2 vb.net windows filesystems

如何使用VB.net获取Windows文件的详细信息?

我所指的详细信息类型是当我右键单击文件(例如MS Word文档),然后单击“属性”并选择“详细信息”选项卡时发现的那些信息。

我知道可以通过FileInfo获得某些信息,但不能全部获得,例如“ Tags”。谢谢

Ňɏs*_*arp 5

对于这些东西,您需要使用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
Run Code Online (Sandbox Code Playgroud)

然后是一个函数来填补它

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
Run Code Online (Sandbox Code Playgroud)

使用它很简单:

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
Run Code Online (Sandbox Code Playgroud)

返回值应该是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)
Run Code Online (Sandbox Code Playgroud)

返回的实际数字将取决于操作系统版本


如注释中所述,Microsoft Shell控件和自动化已重命名为Microsoft Shell文件夹视图路由器(在Windows 8.1中)。

同样,前35个属性是众所周知的,并且比较常见,但是Win7大约有291个。在Windows 8下,最大值是309,其中有一些空白点,并且在列表的深处有些属性索引已更改。

请参阅此答案相关问题如何从.mov视频文件头读取比特率信息