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

时间:2014-10-01 13:36:25

标签: vb.net windows filesystems

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

我的意思是我右键点击一个文件,比如一个MS word doc,然后点击" Properties"并选择"详细信息"标签。

我知道有些可以通过FileInfo获得,但不是全部,例如" Tags"例如。 感谢

3 个答案:

答案 0 :(得分:4)

对于那些东西,你需要使用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返回的。{1}}。 e.g

 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文件夹视图路由器(在Windows 8.1中)。

此外,前35个属性是众所周知的并且更常见,但是对于Win7,大约有291.在Windows 8下,最大值为309,有一些空白点并且深入到列表中,一些属性索引被更改。

查看此答案相关问题How to read the bit rate information from a .mov video file header

答案 1 :(得分:0)

只使用类似:

Dim MyFileInfos as System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo(PathToYourFile)

然后使用MyFileInfo. *(无论需要什么,请使用IntelliSense)获取信息。

祝你有美好的一天

答案 2 :(得分:0)

Dim shellAppType = Type.GetTypeFromProgID("Shell.Application")
Dim shellApp = Activator.CreateInstance(shellAppType)
Dim folder = shellApp.NameSpace("c:\users\frank")
Dim folderitem = folder.parsename("yourfile.jpg")
Dim value = folder.GetDetailsOf(folderitem, 24)  'eg. 24 retrieves File Comments.