递归列出给定路径下的所有文件和文件夹? [英] list recursively all files and folders under the given path?

查看:200
本文介绍了递归列出给定路径下的所有文件和文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何递归列出C#目录中的所有文件?

我想列出给定文件夹(路径)的文件和文件夹的子路径

假设我有文件夹C:\files\folder1\subfolder1\file.txt



如果我给函数c:\files \folder1\

我会得到
subfolder1
subfolder1 \file.txt

  static void Main(string [] args)解决方案

{
DirSearch(@c:\temp);
Console.ReadKey();











$ d $ )
Console.WriteLine(f);
foreach(在Directory.GetDirectories(dir)中的字符串d)
{
Console.WriteLine(d);
DirSearch(d);


$ b catch(System.Exception ex)
{
Console.WriteLine(ex.Message);
}
}


Possible Duplicate:
How to recursively list all the files in a directory in C#?

I want to list the "sub-path" of files and folders for the giving folder (path)

let's say I have the folder C:\files\folder1\subfolder1\file.txt

if I give the function c:\files\folder1\

I will get subfolder1 subfolder1\file.txt

解决方案

Try something like this:

static void Main(string[] args)
{
    DirSearch(@"c:\temp");
    Console.ReadKey();
}

static void DirSearch(string dir)
{
    try
    {
        foreach (string f in Directory.GetFiles(dir))
            Console.WriteLine(f);
        foreach (string d in Directory.GetDirectories(dir))
        {
            Console.WriteLine(d);
            DirSearch(d);
        }

    }
    catch (System.Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

这篇关于递归列出给定路径下的所有文件和文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆