简体   繁体   中英

Save file dialog

Having trouble getting a save file dialog to work with my program. I am using visual studio 2012. The save dialog opens and I can put in a file name but it won't actually save.

       try
                    {
                        //declare a stream writer variables
                        StreamWriter outputfile;

                        //create a file and get a streamwriter object
                        outputfile = File.CreateText("organisms.txt");

                        //write the data to the file
                        for (int b = 0; b < listBox1.Items.Count; b++)
                        {
                            outputfile.WriteLine(listBox1.Items[b].ToString());
                        }

                        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                        {
                            MessageBox.Show("You clicked the save button");
                        }

                        else
                        {
                            MessageBox.Show("You hit the cancel button");
                        }

                        //close the file
                        outputfile.Close();
                    }

I'm going to use pseudo code for this one. The code you're showing is saying that you're making a file called organisms.txt. And um, you're not really using the SaveDialog.

The way you've coded it is that, regardless of whatever the user chose - whether to save it or not - the program will end up writing to organisms.txt.

You need to change the following;

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
     MessageBox.Show("You clicked the save button");
}

Just cut and paste the loop and the stream there. Change organisms.txt with saveFileDialof1.FileName so that it saves it wherever the Save Dialog points to. Although I don't really truly understand StreamWriter that much, so, do help me tweak the code.

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
     MessageBox.Show("You clicked the save button");

     //create a file and get a streamwriter object
     outputfile = File.CreateText(saveFileDialog1.FileName);

     //write the data to the file
     for (int b = 0; b < listBox1.Items.Count; b++)
     {
           outputfile.WriteLine(listBox1.Items[b].ToString());
     }
}

StreamWriter implements IDisposable.
Proper usage would be:

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    MessageBox.Show("You clicked the save button");
    using (StreamWriter outputfile = File.CreateText("organisms.txt")) 
    {
        for (int b = 0; b < listBox1.Items.Count; b++)
        {
            outputfile.WriteLine(listBox1.Items[b].ToString());
        }
    }
}
else
{
    MessageBox.Show("You hit the cancel button");
}

If that still doesn't work:
1) See if any exceptions are being thrown
2) Keep in mind that you are not using the file from the dialog, but 'organisms.txt' which would be saved in the working directory of the app

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM