C#.Net 遍历文件夹(文件&目录),并把所有文件写到txt中时间:2022-12-12 12:26:11//遍历目录,并保存所有文件信息 privat void GetAllFiles(string path,StringCollection tion){DirectoryInfo dir=new DirectoryInfo(path);foreach(FileInfo file in dir.GetFiles(*))//设置文件类型{tion.Add(file.FullName); //网StringCollection里面添加文件名}foreach(DirectoryInfo subDire in dir.GetDirectorys(*) ) //操作子目录{GetAllFiles(subDire.FullName,tion); //递归}} //把文件信息写进txt文件 private void WriteTxt(string path,StringCollection fileNames){ using(StreamWriter sw=new StreamWriter(path)) { foreach(string fileName in fileNames) { sw.WriteLine(fileName); } }}