2

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

3
  • 1
    You can use the Shell API, see Read/Write 'Extended' file properties Oct 1, 2014 at 13:55
  • If you want this specifically for word, you can your the Word.Interop to get/set this info.
    – Steve
    Oct 1, 2014 at 14:12
  • @Steve, nope, ta. Need it for various file types... word, pdf, rtf, xml maybe, etc
    – Toby
    Oct 1, 2014 at 14:39

3 Answers 3

5

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

2
  • 1
    Thanks For Very Good & Working Code. Microsoft Shell Controls and Automation is renamed as Microsoft Shell Folder View Router (in Windows 8.1)
    – user4854145
    May 1, 2015 at 12:58
  • 1
    @Kamlesh thanks for the info - I added it to the answer as well as some other changes in Win 8. May 1, 2015 at 14:56
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.
-1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.