我目前必须编写代码以允许我读取文件夹的所有文件并将它们写入控制台。下面,我还获得了使用浏览器从目录中选择单个文件的代码。我想知道如何使用浏览按钮选择文件夹。
检查所有文件的代码
foreach(var path in Directory.GetFiles(@"C:\Name\Folder\"))
{
Console.WriteLine(path); // full path
Console.WriteLine(System.IO.Path.GetFileName(path)); // file name
}
打开对话框的代码
OpenFileDialog fileSelectPopUp = new OpenFileDialog();
fileSelectPopUp.Title = "";
fileSelectPopUp.InitialDirectory = @"c:\";
fileSelectPopUp.Filter = "All EXCEL FILES (*.xlsx*)|*.xlsx*|All files (*.*)|*.*";
fileSelectPopUp.FilterIndex = 2;
fileSelectPopUp.RestoreDirectory = true;
if (fileSelectPopUp.ShowDialog() == DialogResult.OK)
{
textBox1.Text = fileSelectPopUp.FileName;
}