title: C# move to end of file with StreamWriter created from FileStream
body: <p>I need to create a StreamWriter from a FileStream object and append some text to 
  the file. It is assumed that the FileStream object that is being used has been created with FileMode.OpenOrCreate and FileAccess.ReadWrite. I have:</p>
  
  <pre><code>using (FileStream fs = GetCurrentFileStream())
  {
      StreamWriter sw = new StreamWriter(fs);
      sw.WriteLine("StringToAppend");
      sw.Flush();
  }
  </code></pre>
  
  <p>However this just overwrites the file from the beginning. How do I move to the end of the file? Is there perhaps a way to change the FileMode to Append and FileAccess to Write after the FileStream has been created?</p>
  
  <p>Edit: As mentioned above I need to do this using a FileStream object. The answers from <a href="https://stackoverflow.com/questions/2837020/open-existing-file-append-a-single-line">Open existing file, append a single line</a> assume that I can create a new StreamWriter from the file path which I don't have.</p>
  
  <p>Edit 2: Added truncated version of GetCurrentFileStream().</p>
  
  <pre><code>    public static FileStream GetCurrentFileStream()
      {
          String fileName = getFileName();
          FileStream fs = OpenFileWhenAvailable(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
      }
  
      public static FileStream OpenFileWhenAvailable(String fileName, FileMode fileMode, FileAccess fileAccess, FileShare fileShare)
      {
          int tries = 0;
          int timeout = 10 * 1000;
          while (true)
          {
              tries++;
              try
              {
                  return new FileStream(fileName, fileMode, fileAccess, fileShare);
              }
              catch (IOException ex)
              {
                  if (tries * 100 &gt; timeout)
                  {
                      return null;
                  }
                  else
                  {
                      System.Threading.Thread.Sleep(100);
                  }
              }
          }
      }
  </code></pre>
  
  <p>GetCurrentFileStream is used in several different contexts, so changing the FileMode and FileAccess directly is not an option. I do not wish to make a separate version of GetCurrentFileStream just for this one case, which is why I'm asking if there is a way to jump to the end of the stream and append a string when the FileStream object has already been created.</p>