A displayed entry in the Win10 music library includes the file name, the contributing artist, the album name, the track number, and the song title. I know how to get the file name in Visual Basic using the directory.getfiles method. Is there any way to get the other fields programmatically as well?
Asked
Active
Viewed 111 times
1 Answers
1
This could be done adding a reference to the "Microsoft Shell Controls and Automation" from the COM tab in the project references.
After adding the reference you will be able to instantiate a Shell object
Dim info As List(Of String) = New List(Of String)
Dim shell As Shell32.Shell = New Shell32.Shell()
' Path to the MyMusic folder
Dim musicPath = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic)
Dim musicFolder As Shell32.Folder = shell.NameSpace(musicPath)
At this point you will be able to query each item in that folder and ask for their extended properties
For Each item As Shell32.FolderItem2 In musicFolder.Items()
' Title
Console.WriteLine($"Title: {musicFolder.GetDetailsOf(item, 21)}")
' Author
Console.WriteLine($"Author: {musicFolder.GetDetailsOf(item, 20)}")
' Album
Console.WriteLine($"Album: {musicFolder.GetDetailsOf(item, 14)}")
' Artist
Console.WriteLine($"Part.: {musicFolder.GetDetailsOf(item, 13)}")
' Track
Console.WriteLine($"Track.: {musicFolder.GetDetailsOf(item, 26)}")
' Duration
Console.WriteLine($"Length: {musicFolder.GetDetailsOf(item, 27)}")
' Bits
Console.WriteLine($"Bits: {musicFolder.GetDetailsOf(item, 28)}")
Console.WriteLine()
Next
And for a description of those magic numbers in the call to GetDetailsOf you can take a look at this question: What options are available for Shell32.Folder.GetDetailsOf

Steve
- 213,761
- 22
- 232
- 286
-
Steve, your answer worked just fine for music that is directly in the top folder, e.g., "my music\song file. Can this be made to work for music in sub-folders, e.g.,my music\sub-folder\song file? – user3111176 May 03 '21 at 19:11
-
You just need to change the line where _musicPath_ is defined. You can use [Path.Combine](https://learn.microsoft.com/en-us/dotnet/api/system.io.path.combine?view=net-5.0) to add your specific folder to the base for the MyMusic folder returned by Environment.GetFolderPath. Of course if you need to recurse on every folder then you need a solution that give you a recursive examination of each folder. You can find many examples here or on the net – Steve May 03 '21 at 19:18