How to search for files recursively inside a folder?

Asked

Viewed 633 times

0

I am developing an application, and I need to return the paths of files that have same name and same extension from within a specific folder, to assign a version there are them, but I do not know any method to base. Basically, I would have to search the project’s base folder for the files named Assemblyinfo.Cs, and throw these paths into an array, but I don’t know how to query.
Resolved by : star

  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.
  • 1

    Well, the algorithm involves doing a recursive search of files inside a folder, then comparing names, etc... Which step do you doubt? The question needs to be more specific.

  • Well, I doubt the research step.

  • 1

    I edited the title of the question to better reflect doubt.

1 answer

2


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

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

To search 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 mode search Lazy. So files are found as they are accurate. EnumerateFiles returns IEnumerable<string> instead of string[].

Browser other questions tagged

You are not signed in. Login or sign up in order to post.