C# 不使用递归遍历目录树中的文件和文件夹
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# 不使用递归遍历目录树中的文件和文件夹的更多相关文章
- php递归遍历目录计算其大小(文件包括目录和普通文件)
<?php function countdir($path){ $size = 0; //size = 0; 跟 size = null; 怎么结果不一样 $path = rtrim($path ...
- (实用篇)PHP不用递归遍历目录下所有文件的代码
<?php /** * PHP 非递归实现查询该目录下所有文件 * @param unknown $dir * @return multitype:|multitype:string */ fu ...
- Linux下文件及目录的一些操作(附递归遍历目录源码)
1.获取当前工作目录 #include <unistd.h> 1.char *getcwd(char *buf,size_t size); 2. 3.其中,buf为缓冲区地址,size为给 ...
- Java 之递归遍历目录
Java 之递归遍历目录 一.内容 输出指定目录(文件夹)下的所有文件(包括目录)的绝对路径 二.源代码:RecursiveListDirectory.java package cn.com.zfc. ...
- Java中递归的优缺点,Java写一个递归遍历目录下面的所有文件包括子文件夹里边的文件。
题目: 遍历出aaa文件夹下的文件 首先分析思路: 1.首先判断这个文件夹是否为文件,通过isFile()函数可以判断是否为文件. 2.然后通过isDirectory判断是否为目录. 3.如果是目录就 ...
- php源码之遍历目录下的所有的文件
<?php //遍历目录下的所有的文件 -- 递归调用 // http://www.manongjc.com/article/1495.html function get_all_file1($ ...
- 使用 NIO.2 遍历目录下所有的Java文件
package wellGrounded; import java.io.IOException; import java.nio.file.FileVisitResult; import java. ...
- python递归列出目录及其子目录下所有文件
python递归列出目录及其子目录下所有文件 一.前言 函数的递归,简单来说,就是函数内部调用自己 先举个小例子,求阶乘 def factorial(n): if n == 0: return 1 e ...
- Python 遍历目录下的子目录和文件
import os A: 遍历目录下的子目录和文件 for root,dirs ,files in os.walk(path) root:要访问的路径名 dirs:遍历目录下的子目录 files:遍历 ...
随机推荐
- Python学习笔记-随机数
IronPython的random 只能在0-0.5之间,所以最后调用了C#的Random. #!/usr/bin/python #coding=utf-8 import random import ...
- 用户态驱动--UIO机制的实现【转】
转自:https://blog.csdn.net/u013982161/article/details/51584900 1 uio理论部分 1.1为什么出现了UIO? 硬件设备可以根据功能分为网 ...
- python模块之sniffio
嗅探python用了哪个异步库 from sniffio import current_async_library import trio import asyncio async def print ...
- NODE_ENV不是内部或外部命令,也不是可运行的程序
NODE_ENV不是内部或外部命令,也不是可运行的程序 解决办法:安装across-env:npm install cross-env –save-dev 在运行命令加前缀:在NODE_ENV=xxx ...
- codeforces411div.2
每日CF: 411div2 Solved A CodeForces 805A Fake NP Solved B CodeForces 805B 3-palindrome Solved C CodeFo ...
- IEnumerable和IEnumerator使用
IEnumerable接口是非常的简单,只包含一个抽象的方法GetEnumerator(),它返回一个可用于循环访问集合的IEnumerator对象. IEnumerator是一个真正的集合访问器,没 ...
- Openssl源代码整理学习---含P7/P10/P12说明
声明:建议结合Openssl源代码学习: 一.基础知识 1.Openssl 简史 OpenSSL项目是加拿大人Eric A.Yang 和Tim J.Hudson开发,现在有Openssl项目小组负责改 ...
- centos如何安装Python3
Linux下默认系统自带python2.6的版本,这个版本被系统很多程序所依赖,所以不建议删除,如果使用最新的Python3那么我们知道编译安装源码包和系统默认包之间是没有任何影响的,所以可以安装py ...
- List<T>常用操作
1.List<T>类型强制转换: List<UIData> datalist=null;datalist.ConvertAll<object>(input => ...
- Thread Synchronization Queue with Boost
介绍:当开发一个多线程程序时,同步是一个很大的问题.如果你的程序需要数据流包,那么用队列是个好办法. 你可以在 http://www.boost.org/ 发现 boost 库和文档,从它的网站可以看 ...