C#获取文件夹下所有文件

string fileInfo = string.Empty;
private void GetAllFiles()
{
     if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
     {
DirectoryInfo dInfo = new DirectoryInfo(folderBrowserDialog.SelectedPath);

//遍历该文件夹
GetFolder(dInfo);
     }
     using (System.IO.StreamWriter sw = new StreamWriter
(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"\\fileInfo.txt",false, Encoding.GetEncoding("gb2312")))
     {
sw.Write(fileInfo);
sw.Flush();
     }
}

private void GetFolder(DirectoryInfo dInfo)
{
    
     //显示其中文件
     GetFile(dInfo);
     //遍历文件夹中的文件夹
     foreach (DirectoryInfo dir in dInfo.GetDirectories())
     {
//递归遍历该文件夹
GetFolder(dir);
     }
}

private void GetFile(DirectoryInfo dInfo)
{
     //遍历文件夹中的文件
     foreach (FileInfo file in dInfo.GetFiles())
     {
fileInfo += file.FullName + "\r\n";

Application.DoEvents();
     }
}

你可能感兴趣的:(C#获取文件夹下所有文件)