您的位置:首页 > 编程语言 > C#

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

2015-06-14 21:24 274 查看
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) { } } }}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: