简体   繁体   中英

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

如何在 WinForm(Windows 应用程序)的列表框中列出某个目录(C:\\Users\\Ece\\Documents\\Testings)中的文本文件?

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

To get the txt files, try this:

var folder = @"C:\Users\Ece\Documents\Testings";
var txtFiles = Directory.GetFiles(folder, "*.txt");

listBox.Items.AddRange(txtFiles);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM