none
Retrieving file metadata using VB.Net RRS feed

  • Question

  • Hi!

    I'm using VS2010 Express. I have a VB.net project, where I need to find the metadata of individual files; Artist, Author, Comments, etc. (Specifically I need to find the video length in time of .wtv files).

    I have searched, but could only find VB6 examples and other code snippets that simply didn't work as they required some retired com or dll to do it. How do I do it in the latest VB.net? NOT VB6, C# or anything else. VB.Net in Visual Studio 2010 Express.

    Brgds

    Danny

    Thursday, October 6, 2011 11:24 AM

Answers

  • Hi!

    Thank you all, I have now figured it out and have it working. That there was a KeyValuePair embedded within another KeyValuePair in the sample code threw me for a while in trying to figure out how to extract the keys and values.

    In case someone else needs this in future and comes across this thread, here's the VB.Net code (I know that the variable naming is not per usual naming strategies):

            
    Sub Main()
            Dim FileName as String
            FileName = "C:\temp\testfile.wtv"
            Dim Properties As Dictionary(Of Integer, KeyValuePair(Of String, String)) = GetFileProperties(FileName)
            For Each FileProperty As KeyValuePair(Of Integer, KeyValuePair(Of String, String)) In Properties
                Console.WriteLine("{0}: {1}", FileProperty.Value.Key, FileProperty.Value.Value)
            Next
    End Sub
    
        Public Function GetFileProperties(ByVal FileName As String) As Dictionary(Of Integer, KeyValuePair(Of String, String))
            Dim Shell As New Shell
            Dim Folder As Folder = Shell.[NameSpace](Path.GetDirectoryName(FileName))
            Dim File As FolderItem = Folder.ParseName(Path.GetFileName(FileName))
            Dim Properties As New Dictionary(Of Integer, KeyValuePair(Of String, String))()
            Dim Index As Integer
            Dim Keys As Integer = Folder.GetDetailsOf(File, 0).Count
            For Index = 0 To Keys - 1
                Dim CurrentKey As String = Folder.GetDetailsOf(Nothing, Index)
                Dim CurrentValue As String = Folder.GetDetailsOf(File, Index)
                If CurrentValue <> "" Then
                    Properties.Add(Index, New KeyValuePair(Of String, String)(CurrentKey, CurrentValue))
                End If
            Next
            Return Properties
        End Function
    

    Brgds

    Danny

    • Marked as answer by dbjorck Sunday, October 9, 2011 3:23 PM
    Sunday, October 9, 2011 3:23 PM
  • How are you using that example?  Did you convert it to Visual Basic?
    http://www.developerfusion.com/tools/convert/csharp-to-vb/
    http://msdn.microsoft.com/en-us/magazine/cc163652.aspx

    The open fle dialog is a standard dialog in Visual Basic .Net.  It is listed in the Dialogs section of the toolbox by default.  But you don't need it for your application - simply insert the name of the file that you are trying to get the details for as a string literal at the place in that example code where the filename from the dialog is used.

    • Marked as answer by dbjorck Sunday, October 9, 2011 8:39 AM
    Saturday, October 8, 2011 9:41 PM
  • Here's a C# example.
    • Marked as answer by dbjorck Sunday, October 9, 2011 8:40 AM
    Thursday, October 6, 2011 4:45 PM

All replies

  • Hi,

    Try the links

    http://www.tek-tips.com/viewthread.cfm?qid=1379434&page=1

    Regards,
    A.Murugan

     


    If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Happy Programming!
    Thursday, October 6, 2011 12:00 PM
  • Hi!

    Thank you, but I still could not see anything there talking about the latest VB.net. It was all talking about Windows 2000 and C or C++. It also talked about methods only still existing for backwards compatibility. Which is why I made it a very specific point in my post, because that is also all I could find when I searched the web myself, Windows 2003 was the latest I could find on it. (Which didn't work).

    So again, what I need are instructions on how to read the extended properties, like "Song", "Artist", "Album", etc, the ones you see on the Details tab when you select Properties on a file. In Windows 7 (64 bit), Visual Studio 2010 Express, VB.Net. Not Windows 2000, VB6, or anything else.

    Brgds

    Danny


    • Edited by dbjorck Thursday, October 6, 2011 2:42 PM
    Thursday, October 6, 2011 2:40 PM
  • So again, what I need are instructions on how to read the extended properties, like "Song", "Artist", "Album", etc, the ones you see on the Details tab when you select Properties on a file. In Windows 7 (64 bit), Visual Studio 2010 Express, VB.Net. Not Windows 2000, VB6, or anything else.

    You want the Folder.GetDetailsOf method in shell32.dll.  Reference it by browsing to in the the System32 directory.l
    • Edited by JohnWein Thursday, October 6, 2011 3:19 PM
    Thursday, October 6, 2011 3:17 PM
  • Hi!

    Thank you - but - Sorry for being a stupid nuisance, but I'm still not getting exactly how to do it. I saw GetDetailsOf in MSDN before, but could not get it to work, hence my question (and it also talked about Windows 2003, not 7). Now that you mentioned Reference, I realized I probably needed to add a reference. I have a shell32.dll in the System32 directory, but it does not show up when I try to add a reference to the project in Visual Studio. It does however show one in SysWOW64 whatever that is - I added that, and it helped a little.

    And I tried the sample code in MSDN; which I can't get to work as I need it to. This works:

    Dim objShell
    Dim objFolder
    objShell=CreateObject("shell.application")
    objFolder=objShell.NameSpace("C:\Windows")
    

    But this does not:

    Dim objShell
    Dim objFolder
    Dim myFolder As String = "C:\Windows"
    objShell=CreateObject("shell.application")
    objFolder=objShell.NameSpace(myFolder)
    

    The second example creates an objFolder of Nothing. According to the MSDN article I need to create that before I can use the GetDetailsOf method. I'm afraid I need someone to hold my hand and walk me through it... :-)

    Brgds

    Danny

    Thursday, October 6, 2011 4:22 PM
  • Here's a C# example.
    • Marked as answer by dbjorck Sunday, October 9, 2011 8:40 AM
    Thursday, October 6, 2011 4:45 PM
  • Hi!

    Sorry, still don't see what to do in VB from your C# example. I can find no OpenFileDialog in VB.

    Brgds

    Danny

    Thursday, October 6, 2011 5:10 PM
  • Danny,

    If you're still looking for a solution to this, I have some ideas that I'm experimenting with (actually I have been for a while, then I move to something else, but I'm back on this again).

    I don't know if it'll work with the video type you're talking about though. If I set up a temporary FTP area, would you upload one or two so that I can experiment with?

    Let me know ...

    :)

    Saturday, October 8, 2011 5:55 PM
  • Sorry, still don't see what to do in VB from your C# example. I can find no OpenFileDialog in VB.


    Pretty standard dialog.  I don't know what you would have to do to not find it in VB2010.  It's not necessary though.  It is used only to select the file you want to see the details of.
    • Edited by JohnWein Saturday, October 8, 2011 6:03 PM
    Saturday, October 8, 2011 6:00 PM
  • Sorry, still don't see what to do in VB from your C# example. I can find no OpenFileDialog in VB.


    Pretty standard dialog.  I don't know what you would have to do to not find it in VB2010.  It's not necessary though.  It is used only to select the file you want to see the details of.

    Hi!

    Perhaps it is standard in C#, but I'm not using C#. I'm using VB.net. I looked around using intellisense and help and cannot find a method with that name. There certainly is a similar one, but I don't know which. I'm certainly not an expert able of translating C# to VB.Net. I've never used C# and have only just now started using VB.Net, coming from old VBA environments.

    Brgds

    Danny

     

    Saturday, October 8, 2011 7:47 PM
  • How are you using that example?  Did you convert it to Visual Basic?
    http://www.developerfusion.com/tools/convert/csharp-to-vb/
    http://msdn.microsoft.com/en-us/magazine/cc163652.aspx

    The open fle dialog is a standard dialog in Visual Basic .Net.  It is listed in the Dialogs section of the toolbox by default.  But you don't need it for your application - simply insert the name of the file that you are trying to get the details for as a string literal at the place in that example code where the filename from the dialog is used.

    • Marked as answer by dbjorck Sunday, October 9, 2011 8:39 AM
    Saturday, October 8, 2011 9:41 PM
  • Danny,

    If you're still looking for a solution to this, I have some ideas that I'm experimenting with (actually I have been for a while, then I move to something else, but I'm back on this again).

    I don't know if it'll work with the video type you're talking about though. If I set up a temporary FTP area, would you upload one or two so that I can experiment with?

    Let me know ...

    :)


    Hi!

    Thank you so very much for wanting to try it out!!! Very generous indeed of you. What I want is to get Duration from this file: http://dbjorck.dk/Videos/60%20Minutes_TV2%20DK_2011_10_07_04_11_00.wtv which according to Explorer should be 8 minutes 51 seconds.

    Sometimes my recordings get interrupted for various reasons. I'm trying to write a program to automatically delete these (and a lot of other housekeeping). Who wants to see 8 minutes of 60 Minutes... :-) (Besides it is set to start recording 5 minutes before, so it actually only contains 3 mnutes of it) I have tried looking at file sizes, but they vary too much depending on whether it is HD or SD, and the bitrate also varies.

    Brgds

    Danny

    Sunday, October 9, 2011 7:27 AM
  • How are you using that example?  Did you convert it to Visual Basic?
    http://www.developerfusion.com/tools/convert/csharp-to-vb/
    http://msdn.microsoft.com/en-us/magazine/cc163652.aspx

    The open fle dialog is a standard dialog in Visual Basic .Net.  It is listed in the Dialogs section of the toolbox by default.  But you don't need it for your application - simply insert the name of the file that you are trying to get the details for as a string literal at the place in that example code where the filename from the dialog is used.


    Hi!

    Thank you for the translation link, very helpful.

    I don't have a single thing in the toolbox. I guess that's because I'm using the Express version??

    So the key in all that is this line "Dictionary<int, KeyValuePair<string, string>> fileProps = GetFileProps(ofd.FileName);" and the GetFileProps function, correct? Which translates to "Dim fileProps As Dictionary(Of Integer, KeyValuePair(Of String, String)) = GetFileProps(ofd.FileName)". If I replace ofd.Filename with a string literal of an actual file, I get "Interop type 'ShellClass' cannot be embedded. Use the applicable interface instead." on the statement "Dim shl As Shell = New ShellClass()" in the GetFileProps function.

    I have imported all the libraries mentioned in the code, including shell32 (but on System.Windows.Forms, it says "Namespace or type specified in the Imports 'System.Windows.Forms' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.")

    Brgds

    Danny

     

    Sunday, October 9, 2011 8:22 AM
  • Hi!

    OK, figured it out. Changed it to "Dim shl As New Shell"

    Now I only need to figure out how to use the fileProps Dictionary. I've never used Dictionaries before.

    Brgds

    Danny

    • Proposed as answer by JohnWein Sunday, October 9, 2011 3:30 PM
    Sunday, October 9, 2011 8:39 AM
  • Hi!

    Thank you all, I have now figured it out and have it working. That there was a KeyValuePair embedded within another KeyValuePair in the sample code threw me for a while in trying to figure out how to extract the keys and values.

    In case someone else needs this in future and comes across this thread, here's the VB.Net code (I know that the variable naming is not per usual naming strategies):

            
    Sub Main()
            Dim FileName as String
            FileName = "C:\temp\testfile.wtv"
            Dim Properties As Dictionary(Of Integer, KeyValuePair(Of String, String)) = GetFileProperties(FileName)
            For Each FileProperty As KeyValuePair(Of Integer, KeyValuePair(Of String, String)) In Properties
                Console.WriteLine("{0}: {1}", FileProperty.Value.Key, FileProperty.Value.Value)
            Next
    End Sub
    
        Public Function GetFileProperties(ByVal FileName As String) As Dictionary(Of Integer, KeyValuePair(Of String, String))
            Dim Shell As New Shell
            Dim Folder As Folder = Shell.[NameSpace](Path.GetDirectoryName(FileName))
            Dim File As FolderItem = Folder.ParseName(Path.GetFileName(FileName))
            Dim Properties As New Dictionary(Of Integer, KeyValuePair(Of String, String))()
            Dim Index As Integer
            Dim Keys As Integer = Folder.GetDetailsOf(File, 0).Count
            For Index = 0 To Keys - 1
                Dim CurrentKey As String = Folder.GetDetailsOf(Nothing, Index)
                Dim CurrentValue As String = Folder.GetDetailsOf(File, Index)
                If CurrentValue <> "" Then
                    Properties.Add(Index, New KeyValuePair(Of String, String)(CurrentKey, CurrentValue))
                End If
            Next
            Return Properties
        End Function
    

    Brgds

    Danny

    • Marked as answer by dbjorck Sunday, October 9, 2011 3:23 PM
    Sunday, October 9, 2011 3:23 PM
  • Glad you found your solution.

    :)

    Sunday, October 9, 2011 4:49 PM
  • Thank you again for your willingness to help.

    Brgds

    Danny

    Sunday, October 9, 2011 5:11 PM
  • I don't have a single thing in the toolbox. I guess that's because I'm using the Express version??

    You need to sort that issue out.  You won't be able to write applications uysing VS without the full set of toolbox entries.  There is a Visual Studio installation and setup forum that may be able to assist with a problem such as this, or you could ask in the Express forum.
    Sunday, October 9, 2011 8:23 PM
  • I don't have a single thing in the toolbox. I guess that's because I'm using the Express version??

    Danny,

    When you say you have nothing in the toolbox, do you mean in code view or in form design view?

    Sunday, October 9, 2011 10:05 PM
  • Hi!

    Code View. I didn't have a form, as it was a console application. I've added a form now, and now I have stuff in the toolbox.

    Brgds

    Danny

    Monday, October 10, 2011 6:15 AM
  •  

    I know you have found a solution for this but is this not the same thing

    http://msdn.microsoft.com/en-us/vbasic/bb735955

     

    http://windowsclient.net/learn/video.aspx?v=149505

     

    Just you said you could not find anything on it these are all indirectly linked to the start page of VB 2010

    Wednesday, October 19, 2011 9:33 AM
  • It's now almost 5 years later, but just in case someone else finds this code as I just did.

    Danny's code works great.  I am now using Windows 10 which did not exist at the time.

    Here are a couple of observations:

    The 2 lines from his code assume the index properties are sequential.

            Dim Keys As Integer = Folder.GetDetailsOf(File, 0).Count
            For Index = 0 To Keys - 1

    However, I have found they go as high as 308.  (I don't know if this is max.)  So changing to

           For Index = 0 to 308

    produces more properties, even though Count is much less.  And no error if Count is 50 and loop is 308.

    Also, when I ran this a few months ago using Windows 7, I found that some of the property numbers changed.  For example, previously key# 286 was "Total Bit Rate".  Now #286 is "Shared With".  "Total Bit Rate" is now#308.  Although, in neither instance, did the key have a value, so it may be a moot point.

     


    • Edited by NJNick1 Monday, July 11, 2016 7:52 PM
    Monday, July 11, 2016 7:39 PM