c#打开一个文件夹怎么获得文件夹里的所有子文件夹并自动找到里面的txt文件?

2024-12-03 17:16:00
推荐回答(1个)
回答1:

FolderBrowserDialog dialog = new FolderBrowserDialog();
if (dialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
   string _path = dialog.SelectedPath;
   if (_path.Length > 0)
    {
                    string[] _txtFiles = Directory.GetFiles(_path, "*.txt", SearchOption.AllDirectories);
                    ListBox lstBox = new ListBox();
                    lstBox.Location = new Point(100, 100);
                    lstBox.Size = new System.Drawing.Size(300, 200);
                    lstBox.BringToFront();
                    lstBox.Items.AddRange(_txtFiles);
                    this.Controls.Add(lstBox);    
     }
}

效果图: