title: C# Saving from a Windows form after Creating the File
body: <p>I believe the creating the file finished. I am having issues with saving to a file. What I am supposed to do is create the file initially then fill out the form and have it save to that file and separate them by commas in the file so that in my next assignment I can create a form to read the file and have that fill in the form and split by those commas and fill into text boxes.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using System.IO;
  
  namespace FileExercise
  {
      public partial class frmscout : Form
      {
          private StreamWriter fileWriter;
  
          public frmscout()
          {
              InitializeComponent();
              btnsave.Enabled = false;
          }
  
          private void clickclear(object sender, EventArgs e)
          {
              tb40.Clear();
              tbheight.Clear();
              tbname.Clear();
              tbposition.Clear();
              tbreps.Clear();
              tbverticle.Clear();
          }
  
          private void clickexit(object sender, EventArgs e)
          {
              Application.Exit();
          }
  
          private void clickselect(object sender, EventArgs e)
          {
              DialogResult result;
              string fileName;
  
  
              using (SaveFileDialog fileChooser = new SaveFileDialog())
              {
                  fileChooser.CheckFileExists = false;
                  result = fileChooser.ShowDialog();
                  fileName = fileChooser.FileName;
              }
  
              if (result == DialogResult.OK)
              {
                  if (fileName == string.Empty)
                  {
                      MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                  }
                  else
                  {
                      try
                      {
                          FileStream flstrm = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
                          fileWriter = new StreamWriter( flstrm );
  
                          btnsave.Enabled = true;
                          btnopen.Enabled = false;
                      }
                      catch( IOException )
                      {enter code here
                          MessageBox.Show("Error opening file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                      }
                  }
              }
  
  
  
              //FileStream outfile = new FileStream(fileName, FileMode.Append, FileAccess.Write);
  
  
          }
  
          private void clicksave(object sender, EventArgs e)
          {
              string[] values = new string[6];
  
              values[0] = tbname.Text;
              values[1] = tbheight.Text;
              values[2] = tb40.Text;
              values[3] = tbposition.Text;
              values[4] = tbreps.Text;
              values[5] = tbverticle.Text;
              //}                   
          }
      }
  }
  </code></pre>