C#遍历硬盘所有目录 事件发布模式

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;using System.Text;using System.Threading.Tasks;namespace FileDictoryPath{ public class FoundNewDirectoryEventArgs : System.EventArgs { private object dicFullName; public FoundNewDirectoryEventArgs(object dicFullName) { this.dicFullName = dicFullName; } public object DicFullName { get { return dicFullName;} } } class DirectoryFound { private event EventHandler countChangeEvent; public event EventHandler CountChangeEvent { add { countChangeEvent += value; } remove { countChangeEvent -= value; } } protected virtual void OnNewsDirectoryFound(FoundNewDirectoryEventArgs e) { EventHandler handler = countChangeEvent; if (handler!=null) { handler(this,e); } } private int count; private string path; public int Count { get { return this.count; } } public string Path { get { return this.path; } set { this.count++; FoundNewDirectoryEventArgs args=new FoundNewDirectoryEventArgs(value); OnNewsDirectoryFound(args); } } } class Program { private static StreamWriter sw; private static DirectoryFound directoryFound; static void Main(string[] args) { Console.WriteLine("Check your computer and found all the directories ."); string[] paths = { "A:", "B:", "C:", "D:", "E:", "F:", "G:", "H:", "I:", "J:", "K:", "L:", "M:", "N:", "O:", "P:", "Q:", "R:", "S:", "T:", "U:", "V:", "W:", "X:", "Y:", "Z:" }; Dictionary res=new Dictionary(); foreach (string path in paths) { DirectoryInfo directoryInfo = new DirectoryInfo(path); directoryFound = new DirectoryFound(); directoryFound.CountChangeEvent += NewFound; try { if (directoryInfo.Exists) { String filepath = String.Format("C:\\Driver-{0}.txt", path.Replace(":", "")); FileInfo fileinfo = new FileInfo(filepath); if (!fileinfo.Exists) { fileinfo.Create().Close(); } sw = fileinfo.CreateText(); String dicPath = String.Format("{0}\\", path); GetAllPath(dicPath); lock (sw) { sw.Close(); } Console.WriteLine("File sava as {0}", fileinfo.FullName); res.Add(filepath,directoryFound.Count); Console.WriteLine("Driver:[{0}] Command is Compleate", path); } } catch (Exception) { throw; } } Console.WriteLine("Examining Report --------"); foreach (var r in res) { Console.WriteLine("Driver:[{0}] Founds:[{1}]",r.Key,r.Value); } Console.WriteLine("This Can do more ,just easy . ----"); Console.ReadLine(); } private static void NewFound(object sender, FoundNewDirectoryEventArgs e) { Console.Clear(); Console.WriteLine("News Found:"); Console.WriteLine(e.DicFullName); } //你学的递归呢; private static void GetAllPath(string path) { DirectoryInfo directoryInfo = new DirectoryInfo(path); try { if (directoryInfo.Exists) { foreach (var dicifno in directoryInfo.EnumerateDirectories()) { lock (sw) { sw.WriteLine(dicifno.FullName); } directoryFound.Path = dicifno.FullName; Console.WriteLine("Now is Found {0} Directories 。-Csir",directoryFound.Count); GetAllPath(dicifno.FullName); } } } catch (Exception) { } } }}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值