title: c# Write a line into a text file at specific line number
body: <p>I was searching the web but failed to find the correct example.</p>
  
  <p>The goal is to have a function:</p>
  
  <pre><code>private void InsertLine(string source, string position, string content)
  </code></pre>
  
  <p>And write to a file using the StreamWriter so you do not need to read all lines because I the file is potentially huge.</p>
  
  <p>My function so far:</p>
  
  <pre><code>    private void InsertLine(string source, string position, string content)
      {
          if (!File.Exists(source))
              throw new Exception(String.Format("Source:{0} does not exsists", source));
  
          var pos = GetPosition(position);
          int line_number = 0;
          string line;
          using (var fs = File.Open(source, FileMode.Open, FileAccess.ReadWrite))
                  {
                      var destinationReader = new StreamReader(fs);
                      var writer = new StreamWriter(fs);
                      while (( line = destinationReader.ReadLine()) != null)
                      {
                        if (line_number == pos)
                          {
                              writer.WriteLine(content);
                              break;
                          }                            
                          line_number++;
                      }
                  }
      }
  </code></pre>
  
  <p>The function does not work in the file as nothing happens.</p>