title: C# write an uploaded file to a UNC with FileStream, read it later sometimes doesn't work
body: <p>I've got a rare case where a file cannot be read from a UNC path immediately after it was written. Here's the workflow:</p>
  
  <ol>
  <li>plupload sends a large file in chunks to a WebAPI method</li>
  <li>Method writes the chunks to a UNC path (a storage server). This loops until the file is completely uploaded.</li>
  <li>After a few other operations, the same method tries to read the file again and sometimes it cannot find the file</li>
  </ol>
  
  <p>It only seems to happen after our servers have been idle for a while. If I repeat the upload a few times, it starts to work.</p>
  
  <p>I thought it might be a network configuration issue, or something to do with the file not completely closing before being read again.</p>
  
  <p>Here's part of the code that writes the file (is the filestream OK in this case?)</p>
  
  <pre><code>SaveStream(stream, new FileStream(fileName, FileMode.Append, FileAccess.Write));
  </code></pre>
  
  <p>Here's SaveStream definition:</p>
  
  <pre><code> private static void SaveStream(Stream stream, FileStream fileStream)
      {
          using (var fs = fileStream)
          {
              var buffer = new byte[1024];
  
              var l = stream.Read(buffer, 0, 1024);
              while (l &gt; 0)
              {
                  fs.Write(buffer, 0, l);
                  l = stream.Read(buffer, 0, 1024);
              }
              fs.Flush();
              fs.Close();
          }
      }
  </code></pre>
  
  <p>Here's the code that reads the file:</p>
  
  <pre><code>var fileInfo = new FileInfo(fileName);
  var exists = fileInfo.Exists;
  </code></pre>
  
  <p>It's the <code>fileInfo.Exists</code> that is returning false.</p>
  
  <p>Thank you</p>