title: C# Console App - Streamwriter overwriting CSV file every time it writes new "Page" from API call, only leaving last API call results
body: <p>So, I KNOW this is a simple thing but I typically work with ASP.NET web apps and SQL, so I'm having a problem with this console app. </p>
  
  <p>I have a console app that pulls a "page" of data from a webclient API call. This pulls the data into a string (s) and writes that data to a CSV file. The method then loops, incrementing the "pagenum" by +1  until the API returns the string "No records found". </p>
  
  <p>My problem is, instead of appending each new "page" of data to the existing CSV file, it overwrites the file with the previous "page" of data. So, I write page 1, then get page 2, and all the data in the CSV is replaced with Page 2 data, rinse and repeat.</p>
  
  <p>My question: do I need to create a new "string" for each page of data (maybe incrementing the string variable name by 1 (s1, s2, s3) based on page number or do I simply need to change my loops to use some sort of other part of Streamwriter (like is there an add line command I need to call at the end of the loop?) If so, any ideas on how to do this based on my sample code below? (NOTE: I have some Console.WriteLines for debugging purposes so I can see the data while testing.)</p>
  
  <pre><code>while (!s.Contains("No records found")) //When API returns "No Records Found" we know we've reached the last page of data we need to get.
          {
              rpcURL = "https://api.WEBSITEcom/urlrpc?method=getPlacementReport&amp;username=" + userName + "&amp;password=" + passWord + "&amp;class_code=" + classCode + "&amp;from_date=" + startDate + "&amp;to_date=" + endDate + "&amp;page_num=" + page;
              Console.WriteLine(rpcURL);
              client.Headers.Add("user-agent", "Mozilla/4.0 (Compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
              using (var stream = client.OpenRead(rpcURL))
              using (var sr = new StreamReader(stream))
              {
                  s = sr.ReadToEnd();
  
                  Console.WriteLine(s);
                  string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                  string docPath = path + "\\" + "AleksReport.csv";
  
  
                  if (!s.Contains("No records found"))
                  {
                      using (StreamWriter writer = new StreamWriter(docPath))
                      {
                          writer.WriteLine(s);
                      }
                  }
                  else
                  {
                      Console.WriteLine("End of records");
                  }
              }
              page++;
  
          }
  </code></pre>