Chaos Project

Game Development => Sea of Code => Topic started by: G_G on November 07, 2009, 12:18:46 pm

Title: Quick GetFiles Question
Post by: G_G on November 07, 2009, 12:18:46 pm
Okay so I use this to get every file name in the directory.
string[] fileEntries = Directory.GetFiles(@"C:\");

Now when I do this
foreach (string fileName in fileEntries)
{
    treeView1.Nodes.Add(fileName);
}


It works but I dont want it to display the directory. It displays this in the treeview.
C:\test.txt
C:\test2.txt
C:\test3.txt

I just want it to display the file name and not the directory.
Title: Re: Quick GetFiles Question
Post by: winkio on November 07, 2009, 12:28:20 pm
hmm, I'd use split with "\\" as the delimeter, and then take the last string in the array.
Title: Re: Quick GetFiles Question
Post by: G_G on November 07, 2009, 12:30:59 pm
I dont get what you mean
Title: Re: Quick GetFiles Question
Post by: Blizzard on November 07, 2009, 02:22:31 pm
string[] path = fileEntries.Split("\\");
filename = path[path.Length - 1];
Title: Re: Quick GetFiles Question
Post by: G_G on November 07, 2009, 02:29:10 pm
well that worked, but then what about when I'm in a completely different directory?
C:\Users\Ronnie\Documents\test.txt
Title: Re: Quick GetFiles Question
Post by: Ryex on November 07, 2009, 02:30:38 pm
it should work exactly the same
Title: Re: Quick GetFiles Question
Post by: G_G on November 07, 2009, 02:31:30 pm
wierd when I just tried it it returned this
C:UsersRonnieDocumentstest.txt
Title: Re: Quick GetFiles Question
Post by: Blizzard on November 07, 2009, 04:01:15 pm
The idea was to use it like this:

foreach (string filePath in fileEntries)
{
   string[] path = filePath.Split("\\");
   treeView1.Nodes.Add(path[path.Length - 1]);
}

Title: Re: Quick GetFiles Question
Post by: G_G on November 07, 2009, 04:09:35 pm
That worked thanks blizz!