Welcome to Dream.In.Code
Become an Expert!

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!




Populate TreeView from directory information

2 Pages V  1 2 >  
Reply to this topicStart new topic

Populate TreeView from directory information

PsychoCoder
26 Jan, 2008 - 09:06 AM
Post #1

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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?
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Populate TreeView From Directory Information
26 Jan, 2008 - 09:29 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,280



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
If I'm reading this right, it looks like your inner loop are really processing the results farther up the dir tree.

Perhaps you're aiming for something like this this?

CODE

private void PopulateLibrary() {
    PopulateLibrary(new DirectoryInfo(ROOTDIR));
}

// assumes, rootDir/artistDir/albumDir/songFile
private void PopulateLibrary(DirectoryInfo rootDir) {
    foreach (DirectoryInfo artistDir in rootDir.GetDirectories()) {
        TreeNode artistNode = new TreeNode( artistDir.Name );
        foreach (DirectoryInfo albumDir in artistDir.GetDirectories()) {
            TreeNode albumNode = new TreeNode(albumDir.Name);
            foreach (FileInfo songFile in albumDir.GetFiles()) {
                albumNode.ChildNodes.Add( new TreeNode(songFile.Name) );
            }
            artistNode.ChildNodes.Add(albumNode);
        }
        // Add the nodes
        MediaLibrary.Nodes.Add(artistNode);
    }
    MediaLibrary.CollapseAll();
}


Hope this helps.

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Populate TreeView From Directory Information
26 Jan, 2008 - 11:57 AM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
QUOTE(PsychoCoder @ 26 Jan, 2008 - 09:06 AM) *

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.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Populate TreeView From Directory Information
26 Jan, 2008 - 04:50 PM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
QUOTE(jayman9 @ 26 Jan, 2008 - 11:57 AM) *

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\";


User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Populate TreeView From Directory Information
26 Jan, 2008 - 05:35 PM
Post #5

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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!
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Populate TreeView From Directory Information
26 Jan, 2008 - 05:54 PM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Populate TreeView From Directory Information
26 Jan, 2008 - 06:11 PM
Post #7

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Jay,

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.

Got any ideas?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Populate TreeView From Directory Information
26 Jan, 2008 - 07:04 PM
Post #8

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
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
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Populate TreeView From Directory Information
26 Jan, 2008 - 10:31 PM
Post #9

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Populate TreeView From Directory Information
27 Jan, 2008 - 02:33 AM
Post #10

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Yes the OBJECT tag will work in FF. Make sure you have installed the WMP plugin for FF.

You can get the plugin here.

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.


Attached thumbnail(s)
Attached Image
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Populate TreeView From Directory Information
27 Jan, 2008 - 07:02 AM
Post #11

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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:


CODE

private void LoadSong(string song)
{
    HttpContext.Current.Response.Write(song);
    Player.Filename = song;
    Player.AutoStart = true;
}

protected void MediaLibrary_SelectedNodeChanged(object sender, EventArgs e)
{
    LoadSong(ROOTDIR + this.MediaLibrary.SelectedNode.Parent.Parent.Text.ToString() + "\\" +
    this.MediaLibrary.SelectedNode.Parent.Text.ToString() + "\\" + this.MediaLibrary.SelectedValue);
}



This gives me the path to the file (there has to be a cleaner way OL), but when I click a song I get this pop up in both FF and IE



Attached Image


Its like the .mp3 isn't being seen. Is there a plugin I need for playing MP3's in WMP
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Populate TreeView From Directory Information
27 Jan, 2008 - 10:32 AM
Post #12

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
No plugin needed for MP3's.

I have gotten that message before, but for the life of me I can't remember what it was related to or how to fix it. (damn I hate getting old)

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 04:02PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month