C# SaveFileDialog doesn't create a file and won't close

Viewed 592

I'll start by noting that I'm a complete beginner to C# and I've been trying to "learn by doing."

The overall project is a media player that I'm creating and augmenting from a couple of YouTube videos I've been watching. I'm at a point where I'd like to understand the SaveFileDialog and how to save items from a ListBox into an xml file.

Before I can get there, however, I'm having trouble with just getting the SaveFileDialog to save anything at all and then close.

Here's the code I'm working with:

 public Form1()
        {
            InitializeComponent();
        }
        
        private void btn_save_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "XML-File | *.xml";
            saveFileDialog1.Title = "Save Playlist";
            saveFileDialog1.ShowDialog();

            if (saveFileDialog1.ShowDialog() == DialogResult.OK) ;
        }

Right now, when I click the "Save" button (btn_save), the Save dialog will open and it will default to an xml file, but no file will save after clicking Save, and then after clicking Save, the dialog just opens again and again until I select Cancel.

If I try to add SaveFileDialog.Close(); I get an error saying "SaveFileDialog" does not contain a definition for 'Close', but I figure I've got to set something that tells the dialog to close after hitting Save.

What will help me most here is the "fix" for this and then some comments in the code that explain what's occurring on each line so that I perform more relevant searches and read further.

That said, any help with this would be appreciated. Please let me know if I need to include additional code in this example.

2 Answers

Here's have I would expect code that writes "hello world" to a text file to look like:

private void btn_save_Click(object sender, EventArgs e)
{
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    saveFileDialog1.Filter = "Text file | *.txt";
    saveFileDialog1.Title = "Save Playlist";
    
    if (saveFileDialog1.ShowDialog() != DialogResult.OK) 
      return;

    File.WriteAllText(saveFileDialog1.FileName, "hello world");
}

You can tweak it for XML etc, but the key concepts are:

  • Show the dialog with ShowDialog() - your code will pause at that point while the dialog is open and resume when it closes
  • Inspect the result and if it's not OK (i.e. the user clicked Cancel), return without doing anything
  • Otherwise, do something (like writing a file)

You actually need to save the file with the information obtained from the SaveFileDialog component.

Maybe this could be helpful to you:

public Form1()
{
  InitializeComponent();
}

private void btn_save_Click(object sender, EventArgs e)
{
  Stream myStream ;
  SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  saveFileDialog1.Filter = "XML-File | *.xml";
  saveFileDialog1.Title = "Save Playlist";
  saveFileDialog1.ShowDialog();

  if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
  {
    if((myStream = saveFileDialog1.OpenFile()) != null)
    {
      // Code to write the stream goes here.
      myStream.Close();
    }
  } //end if ShowDialog
} //end Click Button
Related