递归搜索文件/目录 [英] Recursive search Files/Dirs

查看:65
本文介绍了递归搜索文件/目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在搜索文件时遇到问题.
我需要找到"* .gif".在磁盘的所有目录中.

Hello i have a problem with search for file''s.
I need to find e.q. "*.gif". In all directory in disk.

using System;
using System.IO;
using System.Collections.Generic;

class App
{
	static List<string> Files = new List<string>();
	static string Path;
	static void LookFor(string Path)
	{
		string[] Dirs = Directory.GetDirectories(Path);
		for(int i = 0; i < Dirs.Length; i++)
		{
			
		}
	}
	static void Main()
	{
		DriveInfo drive = DriveInfo.GetDrives();
		foreach(DriveInfo drives in drive)
		{
			if(drives.IsReady)
			{
		string[] Dirs = Directory.GetDirectories(drives.Name);
		for(int i = 0; i < Dirs.Length; i++)
		{
			Console.WriteLine("> {0}",Dirs[i].ToString());
			// LookFor();
		}
		}
		}
		Console.ReadKey();
	}
}



并且子目录数存在问题.我不知道它们多少钱.
所以我应该对它使用递归方法+,但是我不知道怎么做;/



And there is problem with number of subdirectories. I dont know how much they are.
So i should use recursive method + for but i dont know how;/
May u help me?

推荐答案

我已经更新了您的代码,以使所有目录都处于递归模式

I have updated your code to get all directories in recursive mode

class App
{
    static List<string> Files = new List<string>();
    static string Path;
    static void LookFor(string Path)
    {
        string[] Dirs = Directory.GetDirectories(Path);
        if (Dirs.Length == 0)
            return;

        for (int i = 0; i < Dirs.Length; i++)
        {
            Console.WriteLine("> {0}\n", Dirs[i].ToString()); 
            // Code here for getting all the *.gif files 
            LookFor(Dirs[i].ToString());
        }
    }
    static void Main()
    {
        DriveInfo[] drive = DriveInfo.GetDrives();
        foreach (DriveInfo drives in drive)
        {
            if (drives.IsReady)
            {
                LookFor(drives.Name);

            }
        }
        Console.ReadKey();
    }
}


尝试一下,

try this,

static void Main()
    {
            foreach (string match in Search("C:\\", "*.gif"))
            {
                Console.WriteLine(match + "");
                //return;
            }
    }


  static IEnumerable<string> Search(string root, string searchPattern)
    {
        Queue<string> dirs = new Queue<string>();
        dirs.Enqueue(root);
        while (dirs.Count > 0)
        {
            string dir = dirs.Dequeue();
            // files             
            string[] paths = null;
            try
            {
                paths = Directory.GetFiles(dir, searchPattern);
            }
            catch
            {
            }
            // swallow              
            if (paths != null && paths.Length > 0)
            {
                foreach (string file in paths)
                {
                    yield return file;
                }
            }
            // sub-directories             
            paths = null;
            try
            {
                paths = Directory.GetDirectories(dir);
            }
            catch
            {
            }
            // swallow              
            if (paths != null && paths.Length > 0)
            {
                foreach (string subDir in paths)
                {
                    dirs.Enqueue(subDir);
                }
            }
        }
    }



像这样,您可以给每个驱动器一个名称.



like this you can give one by one drive name.


这篇关于递归搜索文件/目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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