Click here to Skip to main content
16,004,924 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i have a problem with search for file's.
I need to find e.q. "*.gif". In all directory in disk.

C#
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?
Posted

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

C#
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();
    }
}
 
Share this answer
 
v2
try this,

C#
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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900