我想保存一个新的文件名,但现在我只能保存重写文件。每次我尝试保存新文件名时,都会出现一个带有警告对话框的消息框:
(文件路径)不存在。验证文件名是否正确 给定的“。
以下是我的代码,任何人都可以指出缺少什么?谢谢。
private void button5_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Title = "Save File";
saveFileDialog1.CheckFileExists = true;
saveFileDialog1.CheckPathExists = true;
saveFileDialog1.Filter = "Text files (*.txt)|*.txt| CONF(*.conf)|*.conf|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
// Saves the Image via a FileStream created by the OpenFile method.
System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();
// Saves the Image in the appropriate ImageFormat based upon the
// File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch (saveFileDialog1.FilterIndex)
{
case 1:
saveFileDialog1.FileName = saveFileDialog1.FileName + ".txt";
break;
case 2:
saveFileDialog1.FileName = saveFileDialog1.FileName + ".conf";
break;
default:
saveFileDialog1.FileName = saveFileDialog1.FileName + ".txt";
break;
}
fs.Close();
}
}
答案 0 :(得分:4)
您需要将CheckFileExists
和CheckPathExists
设置为false以防止对话框检查文件是否存在,否则如果用户指定路径,则对话框会显示警告:
saveFileDialog1.CheckFileExists = false;
saveFileDialog1.CheckPathExists = false;