title: Using StreamReader & Writer on same Textfile
body: <p>I recently run into an issue i wasn't able to find a solution for, that suits my needs. So I am trying to read a textfile line by line and check if the line is a specific string. If the file doesn't contain that string yet it should be written to the file. This is my current approche on this.</p>
  
  <pre><code>int i = 0;
  using (var sw = new StreamWriter(this.filePath))
  {
      foreach (SimplePlaylist playlist in myPlaylists)
      {
          this.PlaylistTracks[i] = new List&lt;PlaylistTrack&gt;();
          this.PlaylistTracks[i] = GetPlaylistTracks(playlist.Owner.Id, playlist.Id);
          foreach (PlaylistTrack tr in this.PlaylistTracks[i])
          {
              string write = tr.Track.Name + " // " + string.Join(",", tr.Track.Artists.Select(source =&gt; source.Name)) + " // " + tr.Track.Album.Id;
              string line = "";
              bool found = false;
              using (var sr = new StreamReader(this.filePath))
              {
                  while ((line = sr.ReadLine()) != null)
                  {
                      if (line.Equals(write))
                      {
                          found = true;
                          break;
                      }
                  }
                  sr.Close();
              }
              if (!found)
              {
                  sw.WriteLine(write);
              }
          }
          i++;
      }
      sw.Close();
  }
  </code></pre>
  
  <p>I've read about the problematic of reading and writing to a file at the same time, but i'm wondering if there is a way to achieve this. Any help is appreciated!</p>