Join 149,934 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,490 people online right now. Registration is fast and FREE... Join Now!
Here's what I'm trying to do, I am trying to populate a TreeView but not from an XML file, I'm trying to populate it with the users music library (they provide path, code does the rest). I've tried the following code:
CODE
private void PopulateLibrary() { TreeNode artist; TreeNode album; TreeNode song; //objects to get artists DirectoryInfo dir = new DirectoryInfo(ROOTDIR); //objects to get the albums DirectoryInfo[] subDirs = dir.GetDirectories(); //get the songs FileInfo[] files = dir.GetFiles(); // Add all the sub-directories foreach (DirectoryInfo dirInfo in dir.GetDirectories("*.*")) { // Add the artist node artist = new TreeNode(dirInfo.FullName.Substring(dirInfo.FullName.LastIndexOf(Path.DirectorySeparatorChar) + 1)); foreach (DirectoryInfo sd in subDirs) { album = new TreeNode(sd.Name); artist.ChildNodes.Add(album); for (int i = 0; i < subDirs.Length; i++) { foreach (FileInfo fi in files) { song = new TreeNode(fi.Name); album.ChildNodes.Add(song); } } } // Add the nodes MediaLibrary.Nodes.Add(artist); MediaLibrary.CollapseAll(); } }
And I get an empty TreeView where I'm expecting to see a populated one. Anyone got any ideas on where I'm going wrong? Could it be because the path Ive provided (to get this working) is on an external HDD?
I would say that is most likely the problem, using your code if I set the ROOTDIR to my local drive it populates the TreeView just fine.
Jay, how are you passing the path? I moved some of my music to My Documents/My Music for testing and I still get an empty TreeView, nothing shows up at all. The code Im using is in my first post, I'm setting my path like so
CODE
const string ROOTDIR = @"C:\Documents and Settings\<Name Removed>\My Documents\My Music\";
Thanks guys, got it working, I keep getting C# & VB.Net mixed up, private string ROOTDIR = "K:\Richard\Music\"; will work in VB.Net, but not in C#, for that it has to be private string ROOTDIR = @"K:\\Richard\\Music\\";.
I swear this is something Ive now asked 2 questions on (this project) so there will be a tutorial once I'm done LOL!
I just gave it the absolute path to My Documents and it worked just fine. Tried several other paths on C: drive with the same result.
Here is what I tried the first time:
CODE
const string ROOTDIR = @"C:\Documents and Settings\Jason\My Documents\";
However, interestingly when you posted the path you were using and I tried to access the My Music folder on my computer, I get exactly the same result. The tree never gets populated with data.
In debugging the code, it never enters the first For Each loop, it just skips over it and exits the method.
You have given me something to play with for awhile, let me see what I can find out...
EDIT: looks like you already got it figured out, so NVM. Although that is very interesting.
It also now reads from my external HDD too, so thats good since thats where my 15GB of MP3's are at lol.
Now as you can tell I haven't used the TreeView much, and I was wondering how I would extract the value from a node to play the song. Ill show a screen shot to show how it displays and I want to be able to click on a song in an album under an artist and play the song in the WMP I have embedded.
If you just want the actual value of that Node, then this is how you would do it. That will return the Text value of the selected node.
CODE
this.MediaLibrary.SelectedValue
But I have a feeling that is not what you meant. Correct me if I am wrong.
Wouldn't you need to supply the actual path to the file in order for the embedded WMP to work?
On a side note, I figured out why I wasn't getting any results from My Music folder, all I had in there was a shortcut to another folder. I didn't realize that when I originally looked in there, I don't use that folder for my music. LOL
Yes I, when a song is clicked, need to get the path to the song, so I can set the src of the embedded WMP. Ive tried several methods so far, to no avail as of yet.
I do have a question though, I have created a User Control that is a Windows Media Player control, shows fine in IE, but its not visible in FF, any ideas why? Does FF not support the <object></object> tag?
You will also need to add a reference to WMP to your project. See screen shot.
However, to load WMP in FF is not exactly the same as in IE.
CODE
<!-- Media Player for IE --> <object id="Player1" width="320" height="240" classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" type="application/x-ms-wmp"> <param name="URL" value=""/> </object>
<!-- Media Player for FF --> <object id="Player2" width="320" height="240" type="application/x-ms-wmp"> <param name="URL" value=""/>
I'm thinking that when you build the TreeView you might want to create a Dictionary object that has the filename as the key and the full path as the value. This might solve that problem, since I don't know of anyway to store the path as part of the TreeView.
Ugh..I just realized how late it is...I will take another look at this tomorrow.
Well I need to fix my User Control to change the way the <Object></object tag is written out, but I have a bigger problem. I figured out how to pass the entire path to my player (end up with a path like K:\Richard\Music\Disturbed\The Sickness\11 Droppin' Plates.mp3 , which is the actual path to that song). To do this I actually have 2 methods, one to load the song, the other calls the load method: