Win窗体:SaveFileDialog

发布于 2025-01-07 23:57:28 字数 449 浏览 1 评论 0原文

我将以下代码添加到保存按钮:

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{                
    FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create);
    StreamWriter writer = new StreamWriter(fs);
    writer.Write(twexit.Text);       // twexit is previously created  
    writer.Close();
    fs.Close();
}

当我键入文件名并单击“保存”时,它说该文件不存在。我知道它不存在,但我设置了FileMode.Create。那么,如果文件不存在,不应该创建它吗?

I added the following piece of code to a save button:

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{                
    FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create);
    StreamWriter writer = new StreamWriter(fs);
    writer.Write(twexit.Text);       // twexit is previously created  
    writer.Close();
    fs.Close();
}

When I type the name of the file and click save, it says that file does not exist. I know it does not exist but I set FileMode.Create. So, shouldnt it create file if it does not exist?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倚栏听风 2025-01-14 23:57:28

有一个选项 CheckFileExistsSaveFileDialog 中,如果所选文件不存在,将导致对话框显示该消息。您应该将此设置保留为 false(这是默认值)。

There is an option CheckFileExists in SaveFileDialog which will cause the dialog to show that message if the selected file doesn't exist. You should leave this set to false (this is the default value).

权谋诡计 2025-01-14 23:57:28

您可以简单地使用这个:

File.WriteAllText(saveFileDialog1.FileName, twexit.Text);

而不是使用流的大量代码。它创建新文件或覆盖它。
文件是 System.Io 类。如果你想说文件是否存在,请使用

File.Exist(filePath)

Bye

You can simply use this:

File.WriteAllText(saveFileDialog1.FileName, twexit.Text);

instead of lot of code with stream. It create new file or overwrite it.
File is class of System.Io . If you want to say if file exist, use

File.Exist(filePath)

Bye

谢绝鈎搭 2025-01-14 23:57:28

像这样使用:

     SaveFileDialog dlg = new SaveFileDialog();

        dlg.Filter = "csv files (*.csv)|*.csv";
        dlg.Title = "Export in CSV format";

        //decide whether we need to check file exists
        //dlg.CheckFileExists = true;

        //this is the default behaviour
        dlg.CheckPathExists = true;

        //If InitialDirectory is not specified, the default path is My Documents
        //dlg.InitialDirectory = Application.StartupPath;

        dlg.ShowDialog();
        // If the file name is not an empty string open it for saving.
        if (dlg.FileName != "")

        //alternative if you prefer this
        //if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK
            //&& dlg.FileName.Length > 0)

        {
            StreamWriter streamWriter = new StreamWriter(dlg.FileName);
            streamWriter.Write("My CSV file\r\n");
            streamWriter.Write(DateTime.Now.ToString());
            //Note streamWriter.NewLine is same as "\r\n"
            streamWriter.Write(streamWriter.NewLine);
            streamWriter.Write("\r\n");
            streamWriter.Write("Column1, Column2\r\n");
            //…
            streamWriter.Close();
        }

        //if no longer needed
        //dlg.Dispose();

Use like this:

     SaveFileDialog dlg = new SaveFileDialog();

        dlg.Filter = "csv files (*.csv)|*.csv";
        dlg.Title = "Export in CSV format";

        //decide whether we need to check file exists
        //dlg.CheckFileExists = true;

        //this is the default behaviour
        dlg.CheckPathExists = true;

        //If InitialDirectory is not specified, the default path is My Documents
        //dlg.InitialDirectory = Application.StartupPath;

        dlg.ShowDialog();
        // If the file name is not an empty string open it for saving.
        if (dlg.FileName != "")

        //alternative if you prefer this
        //if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK
            //&& dlg.FileName.Length > 0)

        {
            StreamWriter streamWriter = new StreamWriter(dlg.FileName);
            streamWriter.Write("My CSV file\r\n");
            streamWriter.Write(DateTime.Now.ToString());
            //Note streamWriter.NewLine is same as "\r\n"
            streamWriter.Write(streamWriter.NewLine);
            streamWriter.Write("\r\n");
            streamWriter.Write("Column1, Column2\r\n");
            //…
            streamWriter.Close();
        }

        //if no longer needed
        //dlg.Dispose();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文