How to search for a file in all folders

3

How to create an application that performs a full search on a folder or disk looking for a file that neither in the image



I've already tried to use Directory.GetFiles and Directory.GetDirectory
But when it arrives at a folder in which you do not have access it cancels all the action.

And even so, there was no way to search for a file by name, size, or extension.

In the case of the above image it searches the entire disk for a file called aria2c.exe

And when that application finds this file, it starts right after installation.

    
asked by anonymous 16.07.2015 / 21:56

3 answers

3

If you want to dodge the directories you do not have permission to, and continue searching for the directories you have permission, you will have to implement the search recursively on your own, one directory at a time, and use try/catch calls Directory.GetFiles and Directory.GetDirectories .

public static IEnumerable<string> AcharArquivosComPermissaoRecursivamente(
    string caminhoRaiz,
    string padrao = "*.*")
{
    var caminhosPendentes = new Queue<string>();
    var arquivosAchados = new List<string>();

    caminhosPendentes.Enqueue(caminhoRaiz);

    while (caminhosPendentes.Count > 0)
    {
        var caminhoAtual = caminhosPendentes.Dequeue();

        try
        {
            var listaArquivos = Directory.GetFiles(caminhoAtual, padrao);
            arquivosAchados.AddRange(listaArquivos);

            foreach (var subDiretorio in Directory.GetDirectories(caminhoAtual))
                caminhosPendentes.Enqueue(subDiretorio);
        }
        catch (UnauthorizedAccessException)
        {
            // Ignorar exceções sobre acesso não autorizado.
        }
    }

    return arquivosAchados;
}
    
16.07.2015 / 23:51
2

Basically this is it:

var lista = new DirectoryInfo("c:\").GetFiles("aria2c.exe", SearchOption.AllDirectories);

The secret is the second parameter of GetFiles() that determines the recursive search with enum SearchOption .

If you want to handle access errors on your own and avoid that the abort method can use a solution like the one below. Interestingly Marc Gravel who works at the SE has already given several answers each in a different way, I found is right for you :

public static class FileUtil {
    public static IEnumerable<string> GetFiles(string root, string searchPattern) {
        Stack<string> pending = new Stack<string>();
        pending.Push(root);
        while (pending.Count != 0) {
            var path = pending.Pop();
            string[] next = null;
            try {
                next = Directory.GetFiles(path, searchPattern);                    
            }
            catch { } //aqui você pode colocar log, aviso ou fazer algo útil se tiver problemas
            if(next != null && next.Length != 0)
                foreach (var file in next) yield return file;
            try {
                next = Directory.GetDirectories(path);
                foreach (var subdir in next) pending.Push(subdir);
            }
            catch { } //aqui você pode colocar log, aviso ou fazer algo útil se tiver problemas
        }
    }
}

See working on dotNetFiddle .

    
16.07.2015 / 22:24
1

I think your attempt, using Directory.GetDirectories() is the right start.

If you are beating ahead with access exceptions, you will need Permissions when you are doing - for example, using runas.exe .

    
16.07.2015 / 22:10