Note that there are some explanatory texts on larger screens.

plurals
  1. POResearching efficient searching of text inside a directory and its subdirectories using C#
    primarykey
    data
    text
    <p>I am trying to search for a particular occurrence of a string in some files belonging to a directory. (The search is also performed in the sub directories. Currently, I came up with a solution something like this.</p> <ol> <li>Get all filenames inside a directory and its sub directories.</li> <li>Open files one by one.</li> <li>Search for a particular string</li> <li>If it contains, store filename in an array.</li> <li><p>Continue this till the last file. </p> <pre><code>string[] fileNames = Directory.GetFiles(@"d:\test", "*.txt", SearchOption.AllDirectories); foreach (string sTem in fileNames) { foreach (string line in File.ReadAllLines(sTem)) { if (line.Contains(SearchString)) { MessageBox.Show("Found search string!"); break; } } } </code></pre></li> </ol> <p>I think there can be other methods/approach efficient and speeder than this? Using a batch file? OK. Another, solution is to use findstr (but how to use it directly with C# program without a batch file ? What is the most efficient (or more efficient than what I did?) Code examples are much appreciated!</p> <p>Found out another solution.</p> <pre><code>Process myproc = new Process(); myproc.StartInfo.FileName = "findstr"; myproc.StartInfo.Arguments = "/m /s /d:\"c:\\REQs\" \"madhuresh\" *.req"; myproc.StartInfo.RedirectStandardOutput = true; myproc.StartInfo.UseShellExecute = false; myproc.Start(); string output = myproc.StandardOutput.ReadToEnd(); myproc.WaitForExit(); </code></pre> <p>Is this execution of a process good ? Comments on this too are welcome!</p> <p>According to the @AbitChev's method, a sleek (I don't know if it's efficient!). Anyways, it goes on like this. This one searches all the directory as well as the subdirectories!</p> <pre><code>IEnumerable&lt;string&gt; s = from file in Directory.EnumerateFiles("c:\\directorypath", "*.req", SearchOption.AllDirectories) from str in File.ReadLines(file) //where str.Contains("Text@tosearched2") where str.IndexOf(sSearchItem, StringComparison.OrdinalIgnoreCase) &gt;= 0 select file; foreach (string sa in s) MessageBox.Show(sa); </code></pre> <p>(for case-insensitive search. Maybe that could help someone.) Please comment! Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload