How can I list the text files in a certain directory (C:\Users\Ece\Documents\Testings) in a listbox of a WinForm(Windows application)?
+1
A:
To get the txt files, try this:
string folder = @"C:\Users\Ece\Documents\Testings";
string[] txtfiles = Directory.GetFiles(folder, "*.txt");
listBox.Items.AddRange(txtFiles);
ThePower
2009-08-14 10:54:13
ok to where do I write this? the program does compile but the listbox doesnt show anything
Lady Sour
2009-08-14 11:00:04
Whenever you want to get the code (maybe on a Button_Click event? for a Get Files button?
ThePower
2009-08-14 11:01:09
If you are going to use a button, or a method that will be allowed to be clicked multiple times you must clear the list before filling it again.
ThePower
2009-08-14 11:02:18
Is there any way to list only the file names and dates ?
Lady Sour
2009-08-14 11:08:26
you can ditch the need for the loop by using the listbox's AddRange method with txtfiles
CodeByMoonlight
2009-08-14 11:16:11
Good shout, thanks :-)
ThePower
2009-08-14 11:22:52
+1
A:
// What directory are the files in?...
DirectoryInfo dinfo = new DirectoryInfo(@"C:\TestDirectory");
// What type of file do we want?...
FileInfo[] Files = dinfo.GetFiles("*.txt");
// Iterate through each file, displaying only the name inside the listbox...
foreach( FileInfo file in Files )
{
listbox1.Items.Add(file.Name);
}
// A statement, followed by a smiley face... That oughta do it. ;o)
baeltazor
2009-08-14 11:06:02