Quick GetFiles Question

Started by G_G, November 07, 2009, 12:18:46 pm

Previous topic - Next topic

G_G

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.

winkio

hmm, I'd use split with "\\" as the delimeter, and then take the last string in the array.

G_G


Blizzard

string[] path = fileEntries.Split("\\");
filename = path[path.Length - 1];
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

G_G

well that worked, but then what about when I'm in a completely different directory?
C:\Users\Ronnie\Documents\test.txt

Ryex

it should work exactly the same
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

G_G

wierd when I just tried it it returned this
C:UsersRonnieDocumentstest.txt

Blizzard

The idea was to use it like this:

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

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

G_G