title: Processing Huge Files In C#
body: <p>I have a 4Gb file that I want to perform a byte based find and replace on. I have written a simple program to do it but it takes far too long (90 minutes+) to do just one find and replace. A few hex editors I have tried can perform the task in under 3 minutes and don't load the entire target file into memory. Does anyone know a method where I can accomplish the same thing? Here is my current code:</p>
  
  <pre><code>    public int ReplaceBytes(string File, byte[] Find, byte[] Replace)
      {
          var Stream = new FileStream(File, FileMode.Open, FileAccess.ReadWrite);
          int FindPoint = 0;
          int Results = 0;
          for (long i = 0; i &lt; Stream.Length; i++)
          {
              if (Find[FindPoint] == Stream.ReadByte())
              {
                  FindPoint++;
                  if (FindPoint &gt; Find.Length - 1)
                  {
                      Results++;
                      FindPoint = 0;
                      Stream.Seek(-Find.Length, SeekOrigin.Current);
                      Stream.Write(Replace, 0, Replace.Length);
                  }
              }
              else
              {
                  FindPoint = 0;
              }
          }
          Stream.Close();
          return Results;
      }
  </code></pre>
  
  <p>Find and Replace are relatively small compared with the 4Gb "File" by the way. I can easily see why my algorithm is slow but I am not sure how I could do it better.</p>