标题: C# 扫描目录文件,带进度信息 [打印本页]
作者: Gin_Q 时间: 2021-5-26 15:06 标题: C# 扫描目录文件,带进度信息
本帖最后由 Gin_Q 于 2021-5-26 15:22 编辑
- using System;
- using System.Threading.Tasks;
- using System.IO;
-
- class Program
- {
- static void Main()
- {
- string WorkPath = Directory.GetCurrentDirectory();
- string ResultFileName = "文件名_大小.txt";
- DirectoryInfo di = new DirectoryInfo(WorkPath);
- long TotalFileNumber = 0;
- Task ScanFileTask;
- ScanFileTask = Task.Run(
- async () =>
- {
- using (StreamWriter sw = new StreamWriter(ResultFileName, false, System.Text.Encoding.Default))
- {
- foreach (var fi in di.EnumerateFiles("*", SearchOption.AllDirectories))
- {
- TotalFileNumber++;
- await sw.WriteLineAsync(String.Format("{0} : {1:0.00}MB", Path.Combine(fi.DirectoryName, fi.Name), (double)fi.Length / 1048576));
- }
- }
- }
- );
- Task.Run(
- () =>
- {
- while (ScanFileTask.Status != TaskStatus.RanToCompletion)
- {
- Console.Write("已经扫描过 {0} 个文件\r",TotalFileNumber);
- if (ScanFileTask.Status == TaskStatus.Faulted)
- {
- Console.Write("本次扫描发生了错误!{0}", ScanFileTask.Exception);
- break;
- }
- }
- }
- ).Wait();
- Console.Write("本次扫描完成!发现 {0} 个文件",TotalFileNumber);
- Console.ReadLine();
- }
- }
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |