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);
}
}
}

C# 不使用递归遍历目录树中的文件和文件夹的更多相关文章

  1. php递归遍历目录计算其大小(文件包括目录和普通文件)

    <?php function countdir($path){ $size = 0; //size = 0; 跟 size = null; 怎么结果不一样 $path = rtrim($path ...

  2. (实用篇)PHP不用递归遍历目录下所有文件的代码

    <?php /** * PHP 非递归实现查询该目录下所有文件 * @param unknown $dir * @return multitype:|multitype:string */ fu ...

  3. Linux下文件及目录的一些操作(附递归遍历目录源码)

    1.获取当前工作目录 #include <unistd.h> 1.char *getcwd(char *buf,size_t size); 2. 3.其中,buf为缓冲区地址,size为给 ...

  4. Java 之递归遍历目录

    Java 之递归遍历目录 一.内容 输出指定目录(文件夹)下的所有文件(包括目录)的绝对路径 二.源代码:RecursiveListDirectory.java package cn.com.zfc. ...

  5. Java中递归的优缺点,Java写一个递归遍历目录下面的所有文件包括子文件夹里边的文件。

    题目: 遍历出aaa文件夹下的文件 首先分析思路: 1.首先判断这个文件夹是否为文件,通过isFile()函数可以判断是否为文件. 2.然后通过isDirectory判断是否为目录. 3.如果是目录就 ...

  6. php源码之遍历目录下的所有的文件

    <?php //遍历目录下的所有的文件 -- 递归调用 // http://www.manongjc.com/article/1495.html function get_all_file1($ ...

  7. 使用 NIO.2 遍历目录下所有的Java文件

    package wellGrounded; import java.io.IOException; import java.nio.file.FileVisitResult; import java. ...

  8. python递归列出目录及其子目录下所有文件

    python递归列出目录及其子目录下所有文件 一.前言 函数的递归,简单来说,就是函数内部调用自己 先举个小例子,求阶乘 def factorial(n): if n == 0: return 1 e ...

  9. Python 遍历目录下的子目录和文件

    import os A: 遍历目录下的子目录和文件 for root,dirs ,files in os.walk(path) root:要访问的路径名 dirs:遍历目录下的子目录 files:遍历 ...

随机推荐

  1. php编程 之 php基础二

    1,if...else...语句 规则简介: if 语句 - 在条件成立时执行代码 if...else 语句 - 在条件成立时执行一块代码,条件不成立时执行另一块代码 if...elseif....e ...

  2. NDT 算法和一些常见配准算法

    原文链接:http://ghx0x0.github.io/2014/12/30/NDT-match/ 目前三维配准中用的较多的是ICP迭代算法,需要提供一个较好的初值,同时由于算法本身缺陷,最终迭代结 ...

  3. SpringSecurity权限表达式

    * 当我们想要使用多个权限表达式的时候,是不能直接级联调用的,也就是说,我们只能手写了. @Override protected void configure(HttpSecurity http) t ...

  4. 20165231 预备作业二:学习基础和C语言基础调查

    微信文章感想 读了娄老师微信公众号中的文章,老师给我们的启示首先就是要坚持,万事开头难,但是只要肯坚持就一定会有所成就,不管是学习还是生活方面.其中最有触动的就是减肥了,是我三四年来一直难以完成的目标 ...

  5. win7系统查看硬盘序列号步骤

    1.在开始那里输入cmd,打开命令窗口: 2.输入diskpart,按enter键,进入底盘查看选项: 3.输入list disk,按回车键: list disk:查看电脑上有几块硬盘: 输入sele ...

  6. keepalived的vip无法ping通【原创】

    今天收到redis的keepalived vip无法ping通的告警,查看服务器和服务时发现vip在服务器上,服务也正常.只能在本机ping通,跨网段无法ping通.切换keepalived vip至 ...

  7. http与中文编码传输

    分类: http网络及RFC2012-08-12 15:01 3716人阅读 评论(0) 收藏 举报 urljavascript工具pythonimportjsp 关于http的RFC文档:http: ...

  8. python练习实例

    #!/usr/bin/python # -*- coding: UTF-8 -*- try: fh = open("testfile","w") fh.writ ...

  9. Ex3_7无向图二部图_十一次作业

    (a) 从图中的某个顶点做深度优先遍历,并将不同层的顶点标记为红黑两种颜色,使得每条树边的两个顶点的颜色都不相同,如果遇到一条回边并且两个顶点的颜色都相同则说明图不是二部图. (b)如果存在一个长度为 ...

  10. 17)django-模板的继承与导入

    情况1:通常写页面都有个模板用来框定头部LOGO页面,左侧导航菜单,只有右部的内容不同.如果不使用模板就大量重复工作. 特别如果头部或者左侧导航需要修改或者添加,所有页面都需要修改.django 通过 ...