Fetch phrase within files . Cs from a specific folder using C#

Asked

Viewed 183 times

2

I would like to know how I can find a phrase inside files . Cs of a specific folder.

You don’t need to develop code, I just want to know what/what functions to use and how to use them. I couldn’t find anything related on Google.

  • Do you use any IDE?

  • I’m using Visual Studio 2015.

1 answer

3


You can use the classes Directoryinfo and Fileinfo, as follows:

 DirectoryInfo dirInfo = new DirectoryInfo(@"C:\projetos\projeto\src");
 foreach (FileInfo arquivo in dirInfo.GetFiles("*.cs", SearchOption.AllDirectories))
 {
    string codigoCSharp = File.ReadAllText(arquivo.FullName);
    if (codigoCSharp.Contains("Minha Frase"))
        Console.WriteLine("Encontrado");
 }
  • And what it would look like if inside this folder "C: projects src" I had subfolders that I wanted to add to the search?

  • 2

    Utilize SearchOption.AllDirectories in the second parameter of the method GetFiles . I edited the answer.

  • That’s what I was looking for! Thanks for the answer, it was very helpful.

Browser other questions tagged

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