C#遍历目录下的文件和子目录
1 //打开目录 2 private void BtnClickOpenDirPics(object sender, EventArgs e) 3 { 4 FolderBrowserDialog DirPics = new FolderBrowserDialog(); 5 DirPics.SelectedPath = "D:\\"; 6 7 if (DirPics.ShowDialog() == DialogResult.OK) 8 { 9 string FolderPathName = DirPics.SelectedPath; 10 DirectoryInfo DiFolder = new DirectoryInfo(FolderPathName); 11 ArrayList Lfiles = new ArrayList(); 12 GetAll(DiFolder, ref Lfiles); 14 } 15 } 16 17 private void GetAll(DirectoryInfo dir, ref ArrayList FileList)//搜索文件夹中的文件 18 { 19 FileInfo[] allFile = dir.GetFiles(); 20 foreach (FileInfo fi in allFile) 21 { 22 FileList.Add(fi.FullName); 23 } 24 25 DirectoryInfo[] allDir = dir.GetDirectories(); 26 foreach (DirectoryInfo d in allDir) 27 { 28 GetAll(d, ref FileList); 29 } 30 }