title: Save to file open by openfiledialog (C# 2008)
body: <p>What I am trying to do is most likely very simple but afters spending hours I still cant figure out how to do it correctly. I am able to open a text file using the openfiledialog but cannot figure out to save back to that same file. I would like to also be able to check and see if the file is in use before writing to it. Here is my code for the open and save buttons:</p>
  
  <pre><code> public void openToolStripMenuItem_Click(object sender, EventArgs e)
      {
          //This if statement checks if the user has saved any changes to the list boxes
  
          if (MessageBox.Show(
              "Have you saved your work?\nOpening a new file will clear out all list boxes.",
              "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
          {
              //Clears out the listboxes
              this.itemListBox.Items.Clear();
              this.priceListBox.Items.Clear();
              this.qtyListBox.Items.Clear();
  
              //This will open the file dialog windows to allow the user to chose a file
  
              OpenFileDialog fileDialog = new OpenFileDialog();
              fileDialog.Title = "Harv's Hardware";
              fileDialog.InitialDirectory = Directory.GetCurrentDirectory();
              //File Filter
              fileDialog.Filter = "txt files (*.txt)|*.txt";
              fileDialog.FilterIndex = 2;
              fileDialog.RestoreDirectory = true;
              //This if statement executes is the user hits OK
              if (fileDialog.ShowDialog() == DialogResult.OK)
              {
                  //StreamReader readFile = File.OpenText(fileDialog.FileName);
                  currentFile = new StreamWriter(OpenFileDialog.FileName);
                  String inputString = null;
  
                  while ((inputString = readFile.ReadLine()) != null)
                  {
                      this.itemListBox.Items.Add(inputString);
                      inputString = readFile.ReadLine();
                      this.priceListBox.Items.Add(inputString);
                      inputString = readFile.ReadLine();
                      this.qtyListBox.Items.Add(inputString);
                  }
  
              }
          }
  
      }
  </code></pre>
  
  <p>and save button</p>
  
  <p>//Closes and open files</p>
  
  <pre><code>        //Creates a new saveDialog
          SaveFileDialog saveDialog = new SaveFileDialog();
          saveDialog.ShowDialog();
  
          //Listens to the user input
          StreamWriter writeFile = File.CreateText(saveDialog.FileName);
  
          int indexInteger = 0;
  
          //Writes the actual File
          while (indexInteger &lt; priceListBox.Items.Count)
          {
  
              writeFile.WriteLine(itemListBox.Text);
              writeFile.WriteLine(itemListBox.Text);
              writeFile.WriteLine(qtyListBox.Text);
              indexInteger++;
  
          }
  
      }
  </code></pre>
  
  <p>Thanks for any help!</p>