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. LightOJ - 1245 Harmonic Number (II) 求同值区间的和

    题目大意:对下列代码进行优化 long long H( int n ) {    long long res = 0;    for( int i = 1; i <= n; i++ )      ...

  2. POI 海量数据/大数据文件生成SXSSFWorkbook使用简介

    在之前我们知道处理xls的excel用的workbook是HSSFWorkbook,处理xlsx的excel用的是XSSFWorkbook. 上面两个类导出excel的时候数据会驻留在内存中,所以当数 ...

  3. Python全栈(第一部分)day2

    昨日内容回顾 编译型:一次性将全部代码编译成二进制文件 代表语言: C,C++ 优点:执行效率高 缺点:开发速度慢,不能跨平台 解释型:当程序运行时,从上至下一行一行的解释成二进制 优点:开发速度快, ...

  4. 2.Python list_常用方法总结

    一.创建列表 只要把逗号分隔的不同数据项,使用方括号[],括起来即可, 下标(角标索引)从0开始,最后一个一个元素下标可以写-1 list = ['1' , '2' , '3'] list = []  ...

  5. Perf -- Linux下的系统性能调优工具,第 1 部分【转】

    转自:https://www.ibm.com/developerworks/cn/linux/l-cn-perf1/ Perf 简介 Perf 是用来进行软件性能分析的工具. 通过它,应用程序可以利用 ...

  6. ES6学习笔记三(proxy和reflect)

    proxy用法 // 代理 { let obj={ time:'2017-03-11', name:'net', _r: }; let monitor=new Proxy(obj,{ // 拦截对象属 ...

  7. Python3学习笔记05-数字

    Python 数字数据类型用于存储数值 数字类型不能修改,如果改变数字数据类型的值,将重新分配内存空间 以下实例在变量赋值时 Number 对象将被创建: var1 = 10 var2 = 20 也可 ...

  8. WCF之endpoint的binding属性

    最近在回顾之前做的wcf项目时,发现这个binding的属性有BasicHttpBinding,WSHttpBinding,webHttpBinding等几种方式.但是其中的区别当时未深入研究.现在网 ...

  9. Windows下文件夹扩展名

    回收站.{645ff040-5081-101b-9f08-00aa002f954e} 拨号网络.{992CFFA0-F557-101A-88EC-00DD010CCC48} 打印机.{2227a280 ...

  10. 关于hostapd的调试

    对于hostapd和wpa_supplicant 的调试时,希望显示更多的调试信息. 未改动代码时,可以将hostapd 进程拉起时所跟的参数加上"-dd". 即使这样,也不能满足 ...