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. 2017-2018-2 20165237 实验三《 敏捷开发与XP实践》实验报告

    2017-2018-2 20165237 实验三< 敏捷开发与XP实践>实验报告 实验报告表头: 知识点: 1.XP团队使用现场客户.特殊计划方法和持续测试来提供快速的反馈和全面的交流: ...

  2. TensorFlow学习笔记之--[compute_gradients和apply_gradients原理浅析]

    I optimizer.minimize(loss, var_list) 我们都知道,TensorFlow为我们提供了丰富的优化函数,例如GradientDescentOptimizer.这个方法会自 ...

  3. tkinter模块常用参数python

    1.使用tkinter.Tk()生成主窗口(root = tkinter.Tk()) root.title("标题名")       修改窗体的名字,也可以在创建的时候使用clas ...

  4. 刷题之路第三题--Longest Substring Without Repeating Characters

    问题简介:求给定字符串中最长的字符不重复的字符串的长度 问题详解: 给定一个字符串,寻找给定字符串中包含的最长的字符不重复的字符串的长度 注:答案必须是子字符串,不是子序列 是连续的字符不重复的字符串 ...

  5. LordPE修复从进程dump出来的内存文件

    场景 应急响应中从进程发现被注入了EXE文件,通过processhacker的Memory模块dump出来注入的文件.PE修复后在IDA里反汇编查看这个恶意代码的功能是什么. 解决 LordPE 虚拟 ...

  6. Path Sum I && II & III

    Path Sum I Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that ad ...

  7. python3+selenium框架设计08-进一步实现POM

    之前都是只有一个页面,一个用例.这次两个页面.两个测试用例.其实界面自动化测试最大的难点在于driver的传递,需要保持唯一性.另外就是断言的难点. 修改之前的BaiduPage,新增部分代码 fro ...

  8. 【转】Java HashMap的死循环

    问题的症状 从前我们的Java代码因为一些原因使用了HashMap这个东西,但是当时的程序是单线程的,一切都没有问题.后来,我们的程序性能有问题,所以需要变成多线程的,于是,变成多线程后到了线上,发现 ...

  9. Cnblog-Latex数学公式使用测试

    *默认不支持换行的数学公式 1. \(a+b=c\) $a+b=c$ 2. \[a+b=c\] $$a+b=c$$ 3. $alpha$ $\alpha$ $pi$ $\pi$ 4. $\Gamma$ ...

  10. deque-->collections之#双向消息队列

    deque 双向队列单项队列 方法: append #往右边添加一个appendleft #左边添加clear #清空队列count #看看这个队列里某个元素出现了多少次extend #从右边多个元素 ...