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

╄→гoц情女王★ 提交于 2019-11-29 09:22:42

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

string folder = @"C:\Users\Ece\Documents\Testings";
string[] txtfiles = Directory.GetFiles(folder, "*.txt");

listBox.Items.AddRange(txtFiles);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!