Win窗体:SaveFileDialog
我将以下代码添加到保存按钮:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个选项
CheckFileExists
在SaveFileDialog
中,如果所选文件不存在,将导致对话框显示该消息。您应该将此设置保留为 false(这是默认值)。There is an option
CheckFileExists
inSaveFileDialog
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).您可以简单地使用这个:
而不是使用流的大量代码。它创建新文件或覆盖它。
文件是 System.Io 类。如果你想说文件是否存在,请使用
Bye
You can simply use this:
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
Bye
像这样使用:
Use like this: