Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How about somthing like this</p> <pre><code>var found = false; string file; foreach (file in Directory.EnumerateFiles( "d:\\tes\\", "*.txt", SearchOption.AllDirectories)) { foreach(var line in File.ReadLines(file)) { if (line.Contains(searchString)) { found = ture; break; } } if (found) { break; } } if (found) { var message = string.Format("Search string found in \"{0}\".", file) MessageBox.Show(file); } </code></pre> <p>This has the advantage of loading only what is required into memory, rather than the names of all the files then, the contents of each file.</p> <hr> <p>I note you are using <a href="http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx" rel="nofollow"><code>String.Contains</code></a> which</p> <blockquote> <p>performs an ordinal (case-sensitive and culture-insensitive) comparison</p> </blockquote> <p>This would allow us to do a simple charachter wise compare.</p> <p>I'd start with a little helper function</p> <pre><code>private static bool CompareCharBuffers( char[] buffer, int headPosition, char[] stringChars) { // null checking and length comparison ommitted var same = true; var bufferPos = headPosition; for (var i = 0; i &lt; stringChars.Length; i++) { if (!stringChars[i].Equals(buffer[bufferPos])) { same = false; break; } bufferPos = ++bufferPos % (buffer.Length - 1); } return same; } </code></pre> <p>Then I'd alter the previous algorithm to use the function like this.</p> <pre><code>var stringChars = searchString.ToCharArray(); var found = false; string file; foreach (file in Directory.EnumerateFiles( "d:\\tes\\", "*.txt", SearchOption.AllDirectories)) { using (var reader = File.OpenText(file)) { var buffer = new char[stringChars.Length]; if (reader.ReadBlock(buffer, 0, buffer.Length - 1) &lt; stringChars.Length - 1) { continue; } var head = 0; var nextPos = buffer.Length - 1; var nextChar = reader.Read(); while (nextChar != -1) { buffer[nextPos] = (char)nextChar; if (CompareCharBuffers(buffer, head, stringChars)) { found = ture; break; } head = ++head % (buffer.Length - 1); if (head == 0) { nextPos = buffer.Length - 1; } else { nextPos = head - 1; } nextChar = reader.Read(); } if (found) { break; } } } if (found) { var message = string.Format("Search string found in \"{0}\".", file) MessageBox.Show(file); } </code></pre> <p>this holds only as many <code>char</code>s as the search string contains in memory and uses rolling buffer across each file. Theoretically the file could contain no new lines and consume your whole disk, or, your search string could contain a new line.</p> <hr> <p>As further work I'd convert the per file part of the algorithm into a function and investigate a multi-threaded approach.</p> <p>So this would be the internal function,</p> <pre><code>static bool FileContains(string file, char[] stringChars) { using (var reader = File.OpenText(file)) { var buffer = new char[stringChars.Length]; if (reader.ReadBlock(buffer, 0, buffer.Length - 1) &lt; stringChars.Length - 1) { return false; } var head = 0; var nextPos = buffer.Length - 1; var nextChar = reader.Read(); while (nextChar != -1) { buffer[nextPos] = (char)nextChar; if (CompareCharBuffers(buffer, head, stringChars)) { return true; } head = ++head % (buffer.Length - 1); if (head == 0) { nextPos = buffer.Length - 1; } else { nextPos = head - 1; } nextChar = reader.Read(); } return false; } } </code></pre> <p>Then you could process the files in parallel like this</p> <pre><code>var stringChars = searchString.ToCharArray(); if (Directory.EnumerateFiles( "d:\\tes\\", "*.txt", SearchOption.AllDirectories) .AsParallel() .Any(file =&gt; FileContains(file, stringChars))) { MessageBox.Show("Found search string!"); } </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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