前提现在一个分区下建立bb.txt文件。
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.IO; 10 11 namespace WindowsFormsApplication2 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 20 public string foldPath = ""; 21 22 23 public static void iterDir(FileSystemInfo[] fsInfo) 24 { 25 foreach(FileSystemInfo fsi in fsInfo) 26 { 27 if (fsi.Extension.Length <= 0) 28 { 29 FileSystemInfo[] ff = (new DirectoryInfo(fsi.FullName)).GetFileSystemInfos(); 30 iterDir(ff); 31 } 32 else 33 { 34 FileStream fs = new FileStream("E:\\bb.txt",FileMode.Append); 35 StreamWriter sw = new StreamWriter(fs); 36 sw.WriteLine(fsi.FullName); 37 sw.Close(); 38 } 39 40 } 41 } 42 43 44 45 private void btnChoice_Click(object sender, EventArgs e) 46 { 47 FolderBrowserDialog fbd = new FolderBrowserDialog(); 48 fbd.Description = "请选择文件夹"; 49 50 if (fbd.ShowDialog() == DialogResult.OK) 51 { 52 foldPath = fbd.SelectedPath; 53 MessageBox.Show("您选择的文件夹是:"+ foldPath, "选择文件夹提示"); 54 } 55 56 } 57 58 private void btnIterator_Click(object sender, EventArgs e) 59 { 60 DialogResult result; 61 62 DirectoryInfo dirInfo = new DirectoryInfo(foldPath); 63 FileSystemInfo[] fsies = dirInfo.GetFileSystemInfos(); 64 65 iterDir(fsies); 66 67 result = MessageBox.Show("文件夹"+ foldPath+"已经遍历完毕,结果保存在E:\\bb.txt,点击确定打开"," 结果提示",MessageBoxButtons.OKCancel); 68 69 if (result == DialogResult.OK) 70 { 71 System.Diagnostics.Process.Start(@"E:\bb.txt"); 72 } 73 74 75 } 76 } 77 }
C#遍历文件夹(包括子目录)下的所有文件,布布扣,bubuko.com
时间: 2024-12-07 09:42:18