A problem with segmented file write
<p>I download a file's parts (a picture), and then I want to save these parts into one file.</p>

<p>The problem is, that the first part is being downloaded and saved properly (I can see the part of that pricture). But, when the second part is saved (FileMode.Append) the picture seems to be broken.</p>

<p>Here's the code:</p>

<pre><code>  HttpWebRequest webRequest;
  HttpWebResponse webResponse;
  Stream responseStream;
  long StartPosition, EndPosition;

        if (File.Exists(LocalPath))
            fileStream = new FileStream(LocalPath, FileMode.Append);
        else fileStream = new FileStream(LocalPath, FileMode.Create);

        webRequest = (HttpWebRequest)WebRequest.Create(FileURL);

        webResponse = (HttpWebResponse)webRequest.GetResponse();
        responseStream = webResponse.GetResponseStream();

        StartPosition = 0;   //download first 52062 bytes of the file
        EndPosition = 52061;

        webRequest.AddRange(StartPosition, EndPosition);

        int SeekPosition = (int)StartPosition;

        while ((bytesSize = responseStream.Read(Buffer, 0, Buffer.Length)) &gt; 0)
        {
            lock (fileStream)
            {
                fileStream.Seek(SeekPosition, SeekOrigin.Begin);
                fileStream.Write(Buffer,0, bytesSize);
            }

  //the Buffer.Length is 2048.
  //When the bytes count to download is &lt; 2048 then I decrease the Buffer.Length
  //to prevent downloading more that 52062 bytes.

            DownloadedBytesCount += bytesSize;
            SeekPosition += bytesSize;

            long TotalToDownload = EndPosition - StartPosition;

            long bytesLeft = TotalToDownload - DownloadedBytesCount;

            if (bytesLeft &lt; Buffer.Length)
                Buffer = new byte[bytesLeft];
        }
</code></pre>

<p>WHen I want to download the second part of the file I set</p>

<pre><code>    StartPosition = 52062;
    EndPosition = 104122;
</code></pre>

<p>and then there is a problem that I described above. Why the file is not appened properly ?</p>
