title: C# FileStream creating new .csv file but not writing to it
body: <p>Hi I'm trying to write data to a .csv file. My code is creating the new file but isn't writing to it. Here is a shorter version of the code with the name, age, and sex variables already predetermined. In my actual application these variables come from textboxes (I have made sure that the data from the textboxes is actually filling these variables using a MessageBox).</p>
  
  <p><strong>Here is the code:</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.IO;
  
  namespace ConsoleApp1
  {
      class Program
      {
          static void Main(string[] args)
          {
              string filePath = "C:\\Users\\Jamie\\Desktop\\test.csv";
              string name = "jamie";
              int age = 69;
              string sex = "male";
  
              if (!(File.Exists(filePath)))
              {
                  FileStream fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write);
                  if (fs.CanWrite)
                  {
                      string columnTitles = "name,age,sex\n";
                      fs.Write(Encoding.ASCII.GetBytes(columnTitles), 0, 0);
                      string input = name + "," + age + "," + sex + "\n";
                      fs.Write(Encoding.ASCII.GetBytes(input), 0, 0);
                      fs.Flush();
                      fs.Close();
                  }
              }
              else
              {
                  FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write);
                  if (fs.CanWrite)
                  {
                      if (new FileInfo(filePath).Length == 0)
                      {
                          string columnTitles = "name,age,sex\n";
                          fs.Write(Encoding.ASCII.GetBytes(columnTitles), 0, 0);
                      }
                      string input = name + "," + age + "," + sex + "\n";
                      fs.Write(Encoding.ASCII.GetBytes(input), 0, 0);
                      fs.Flush();
                      fs.Close();
                  }
              }
          }
      }
  }
  </code></pre>