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:
// 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)
回答2:
To get the txt files, try this:
string folder = @"C:\Users\Ece\Documents\Testings";
string[] txtfiles = Directory.GetFiles(folder, "*.txt");
listBox.Items.AddRange(txtFiles);