Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
915 views
in Technique[技术] by (71.8m points)

vb.net - Get extended file information details

How can one obtain the details of a windows file using VB.net?

The type of details I mean are those found when I right click on a file, say an MS word doc, then click "Properties" and select the "Details" tab.

I know some can be obtained via FileInfo, but not all, such as "Tags" for example. Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For that stuff you need to use Shell32. From the COM tab, find and add Microsoft Shell Controls and Automation. Here is code to create a list of property-values for a given file:

' 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

Then a function to fill it up

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

Using it is simple:

Dim xtd As List(Of ShellInfo) = GetXtdShellInfo("C:TempCapri.jpg")
For Each s As ShellInfo In xtd
    Console.WriteLine("{0}: {1}", s.Name, s.Value)
Next

The return should be a list of ShellInfo items where the Name is the property name such as Name, BitRate, Album and the associated Value will be that returned by Shell32. 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)

The actual number returned will vary depending on the OS ver


As noted in the comment Microsoft Shell Controls and Automation is renamed as Microsoft Shell Folder View Router (in Windows 8.1).

Also, the first 35 properties are fairly well known and more common, but with Win7 there are about 291. Under Windows 8, the max is 309 with some blank spots and deep into the list some property indices are changed.

See this answer related question How to read the bit rate information from a .mov video file header


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...