How to search for files recursively inside a folder?

0

I'm developing an application, and I need to return the paths of files that have the same name and same extension from within a specific folder, to assign a version there, but I do not know any method to take as a basis. Basically, I would have to look in the project's home directory for the files named Assemblyinfo.cs, and throw those paths into an array, but I do not know how to do the query. Resolved by: dcastro

  var files = Directory.GetFiles("C://caminho_de_amostra", "AssemblyInfo.cs", SearchOption.AllDirectories);
//Pesquisa no caminho atribuido o nome "Assemblyinfo.cs"
            for (int h = 0; h < files.Length; h++)
            {
                Console.WriteLine(files[h]);// Apenas para ter um texto mostrando qual arquivo foi modificado
                File.WriteAllLines(files[h], File.ReadAllLines(files[h]).Select(s => !s.Trim().StartsWith("[assembly: AssemblyFileVersion(") ? s : string.Join(".", s.Trim(']').Trim(')').Trim('"').Split('.').Select((n, i) => i != 3 ? n : !int.TryParse(n, out num) ? n : (anum))) + "\")]"));
            }// Modifica a versão arquivo por arquivo.
    
asked by anonymous 02.04.2015 / 13:15

1 answer

2

To resurrect all files within a directory (and sub-directories), use Directory.GetFiles with the SearchOptions.AllDirectories option.

 var files = Directory.GetFiles(@"c:/folder", "*", SearchOption.AllDirectories)

To search for only files named AssemblyInfo.cs :

 var files = Directory.GetFiles(@"c:/folder", "AssemblyInfo.cs", SearchOption.AllDirectories)

You can also use EnumerateFiles instead of GetFiles to do the lazy mode search. Thus, the files are found as they are being accurate. EnumerateFiles returns IEnumerable<string> instead of string[] .

    
02.04.2015 / 13:52