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