Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list text files in the selected directory in a listbox?

How can I list the text files in a certain directory (C:\Users\Ece\Documents\Testings) in a listbox of a WinForm(Windows application)?

like image 890
Lady Sour Avatar asked Aug 14 '09 10:08

Lady Sour


People also ask

How do I get a list of files in a folder from text?

Right-click that folder and select Show more options. Click Copy File List to Clipboard on the classic menu. You'll still need to paste the copied list into a text file. Launch Run, type Notepad in the Open box, and click OK.

How do I get a list of text files?

You can use dir /b > files. txt from the command-line to get the list of filenames stored into files. txt .


1 Answers

// 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)

like image 91
jay_t55 Avatar answered Oct 18 '22 02:10

jay_t55