Get extended file information details

前端 未结 2 1149
夕颜
夕颜 2020-12-11 10:40

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 clic

相关标签:
2条回答
  • 2020-12-11 11:19

    Just use something like :

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

    Then get infos using MyFileInfo.* (whatever you need, use IntelliSense).

    Have a nice day

    0 讨论(0)
  • 2020-12-11 11:39

    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:\Temp\Capri.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

    0 讨论(0)
提交回复
热议问题