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) { } } }}
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) { } } }}
相关文章推荐
- C#.NET编程系列讲座
- C# 中的 Static
- C#高级编程十一天----编程规则
- C#高级编程第十天----C#预处理器
- [深入学习C#]LINQ查询表达式详解(2)——查询表达式的转换
- C#.NET里面抽象类和接口有什么区别?
- .NET C#获取当前网页地址信息
- C#使用OleDB操作ACCESS插入数据时提示:至少一个参数没有被指定值。
- C#使用OleDB操作ACCESS插入数据时提示:参数 @p_Contract 没有默认值
- C#使用OleDB操作ACCESS插入数据时提示:标准表达式中数据类型不匹配。
- C#高级二
- C# 之【线程与进程】
- C#高级一
- C#入门基础三四
- C#入门基础三
- C# Linq 交集、并集、差集、去重
- C#入门基础二
- C#基础入门一
- C#打开/保存文件,获取路径
- C#操作txt文件