简体   繁体   English

保存文件对话框

[英]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. 我正在使用Visual Studio2012。将打开“保存”对话框,可以输入文件名,但实际上不会保存。

       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. 您显示的代码表明您正在创建一个名为organs.txt的文件。 And um, you're not really using the SaveDialog. 嗯,您并不是真正在使用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. 您编写代码的方式是,无论用户选择了什么(无论是否保存),该程序最终都会写入organs.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. 使用saveFileDialof1.FileName更改organisms.txt saveFileDialof1.FileName以便将其保存在“保存”对话框指向的任何位置。 Although I don't really truly understand StreamWriter that much, so, do help me tweak the code. 尽管我并没有真正真正了解StreamWriter,但是,请帮助我调整代码。

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. StreamWriter实现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 1)查看是否抛出任何异常
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 2)请记住,您不是使用对话框中的文件,而是使用“ organisms.txt”文件,该文件将保存在应用程序的工作目录中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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