public class StackBasedIteration
{
static void Main(string[] args)
{
// Specify the starting folder on the command line, or in
// Visual Studio in the Project > Properties > Debug pane.
TraverseTree(args[]); Console.WriteLine("Press any key");
Console.ReadKey();
} public static void TraverseTree(string root)
{
// Data structure to hold names of subfolders to be
// examined for files.
Stack<string> dirs = new Stack<string>(); if (!System.IO.Directory.Exists(root))
{
throw new ArgumentException();
}
dirs.Push(root); while (dirs.Count > )
{
string currentDir = dirs.Pop();
string[] subDirs;
try
{
subDirs = System.IO.Directory.GetDirectories(currentDir);
}
// An UnauthorizedAccessException exception will be thrown if we do not have
// discovery permission on a folder or file. It may or may not be acceptable
// to ignore the exception and continue enumerating the remaining files and
// folders. It is also possible (but unlikely) that a DirectoryNotFound exception
// will be raised. This will happen if currentDir has been deleted by
// another application or thread after our call to Directory.Exists. The
// choice of which exceptions to catch depends entirely on the specific task
// you are intending to perform and also on how much you know with certainty
// about the systems on which this code will run.
catch (UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
continue;
}
catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
continue;
} string[] files = null;
try
{
files = System.IO.Directory.GetFiles(currentDir);
} catch (UnauthorizedAccessException e)
{ Console.WriteLine(e.Message);
continue;
} catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
continue;
}
// Perform the required action on each file here.
// Modify this block to perform your required task.
foreach (string file in files)
{
try
{
// Perform whatever action is required in your scenario.
System.IO.FileInfo fi = new System.IO.FileInfo(file);
Console.WriteLine("{0}: {1}, {2}", fi.Name, fi.Length, fi.CreationTime);
}
catch (System.IO.FileNotFoundException e)
{
// If file was deleted by a separate application
// or thread since the call to TraverseTree()
// then just continue.
Console.WriteLine(e.Message);
continue;
}
} // Push the subdirectories onto the stack for traversal.
// This could also be done before handing the files.
foreach (string str in subDirs)
dirs.Push(str);
}
}
}

最新文章

  1. Atitit dsl exer v3 qb3 新特性
  2. 【Selenium2+Python】定位
  3. 不同操作系统上屏蔽oracle的操作系统认证方式
  4. singleton(单件)-对象创建型模式
  5. expect入门--自动化linux交互式命令
  6. linux启动jmeter,执行./jmeter.sh报错解决方法
  7. javaSE第四天
  8. php创建文件夹后设置文件夹权限(转)
  9. js实现数组内元素随机排序
  10. webapi文档
  11. wcf事务(随记)
  12. linux权限---【600,644,700,755,711,666,777】 - - 博客频道 - CSDN.NET
  13. koa-router中路由/后面不填参数就会报404的解决办法
  14. 解决laydate时间日期插件定位溢出
  15. JQuery制作基础的无缝轮播与左右点击效果
  16. spring源码解析2--容器的基本实现
  17. Bootstrap3基础 栅格系统 col-md-push/pull 向左、右的浮动偏移
  18. 2013-2015 Aaronyang的又一总结,牧童遥指纳尼村
  19. IAR中的 identifier &quot;FILE&quot; is undefined 问题
  20. memcached(一):linux下memcached安装以及启动

热门文章

  1. 求两个排序数组中位数 C++
  2. 线程池-Executors
  3. mybatis 三剑客 generator配置 、mybatis plugin
  4. Thymeleaf相关补充
  5. 【转】PEP8 规范
  6. FAT文件系统规范v1.03学习笔记---1.保留区之 Fat32 FSInfo扇区结构和备份启动扇区
  7. for循环查找元素怎么跳出for循环
  8. 设计模式C++学习笔记之四(Multition多例模式)
  9. 在operator =中要处理“自我赋值”
  10. 使用vue-cli初始化vue项目