title: Reading file after writing it
body: <p>I have a strange problem. So my code follows as following.</p>
  
  <ol>
  <li><p>The exe takes some data from the user</p></li>
  <li><p>Call a web service to write(and create CSV for the data) the file at perticular network location(say \some-server\some-directory).
  Although this web service is hosted at the same location where this
  folder is (i.e i can also change it to be c:\some-directory). It then
  returns after writing the file</p></li>
  <li><p>the exe checks for the file to exists, if the file exists then further processing else quite with error.</p></li>
  </ol>
  
  <p>The problem I am having is at step 3. When I try to read the file immediately after it has been written, I always get file not found exception(but the file there is present). I do not get this exception when I am debugging (because then I am putting a delay by debugging the code) or when <code>Thread.Sleep(3000)</code> before reading the file.</p>
  
  <p>This is really strange because I close the <code>StreamWriter</code> before I return the call to exe. Now according to the documention, close should force the flush of the stream. This is also not related to the size of the file. Also I am not doing Async thread calls for writing and reading the file. They are running in same thread serially one after another(only writing is done by a web service and reading is done by exe. Still the call is serial)</p>
  
  <p>I do not know, but it feels like there is some time difference between the file actually gets written on the disk and when you do <code>Close()</code>. However this baffling because this is not at all related to size. This happens for all file size. I have tried this with file with 10, 50, 100,200 lines of data.</p>
  
  <p>Another thing which I suspected was since I was writing this file to a network location, it could be windows is optimizing the call by writing first to cache and then to network location. So I went ahead and changed the code to write it on drive(i.e use c:\some-directory), rather than network location. But it also resulted in same error.</p>
  
  <p>There is no error in code(for reading and writing). As explained earlier, by putting a delay, it starts working fine. Some other useful information</p>
  
  <ol>
  <li>The exe is .Net Framework 3.5</li>
  <li>Windows Server 2008(64 bit, 4 GB Ram)</li>
  </ol>
  
  <p><strong>Edit 1</strong>
  <code>File.AppendAllText()</code> is not correct solution, as it creates a new file, if it does not exits</p>
  
  <p><strong>Edit 2</strong>
  code for writing</p>
  
  <pre><code>using (FileStream fs = new FileStream(outFileName, FileMode.Create))
          {
              using (StreamWriter writer = new StreamWriter(fs, Encoding.Unicode))
              {
                  writer.WriteLine(someString)
              }
          }
  </code></pre>
  
  <p>code for reading</p>
  
  <pre><code>  StreamReader rdr = new StreamReader(File.OpenRead(CsvFilePath));
    string header = rdr.ReadLine();
    rdr.Close();
  </code></pre>
  
  <p><strong>Edit 3</strong>
  used textwriter, same error</p>
  
  <pre><code>  using (TextWriter writer = File.CreateText(outFileName))
      {
      }
  </code></pre>
  
  <p><strong>Edit 3</strong>
  Finally as suggested by some users, I am doing a check for the file in while loop for certain number of times before I throw the exception of file not found. </p>
  
  <pre><code>int i = 1;
  while (i++ &lt; 10)
  {
     bool fileExists = File.Exists(CsvFilePath);
     if (!fileExists)
       System.Threading.Thread.Sleep(500);
     else
       break;
  }
  </code></pre>