- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace ConsoleApplication7
- {
- class Program
- {
- static void Main(string[] args)
- {
- System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
- stopwatch.Start();
- List<string> allFileName = new List<string>();
- string[] drives = Directory.GetLogicalDrives();
- Stack<string> stack = new Stack<string>();
- foreach (string drive in drives)
- {
- stack.Push(drive);
- }
- string path;
- DirectoryInfo sDir;
- FileInfo[] fileArray;
- DirectoryInfo[] subDirArray;
- while (stack.Count > 0)
- {
- path = stack.Pop();
- sDir = new DirectoryInfo(path);
- try
- {
- fileArray = sDir.GetFiles();
- foreach (FileInfo file in fileArray)
- {
- allFileName.Add(file.FullName);
- }
- }
- catch (Exception e)
- {
- }
- try
- {
- subDirArray = sDir.GetDirectories();
- foreach (DirectoryInfo subDir in subDirArray)
- {
- stack.Push(subDir.FullName);
- }
- }
- catch (Exception e)
- {
- }
- }
- stopwatch.Stop();
- Console.WriteLine(stopwatch.Elapsed);
- Console.WriteLine(allFileName.Count);
- Console.ReadLine();
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1706201614737.html
来源: http://www.codesnippet.cn/detail/1706201614737.html