我正在显示一个 SaveFileDialog,当单击 OK 时,我正在创建新文件,向其中写入一些默认内容,然后尝试通过 SaveFileDialog 的 OpenFile() 方法打开它。但是,当我调用 OpenFile() 时,文件的内容就会被删除。
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "XML files (*.xml)|*.xml";
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// First Event Creates file and writes default content to it - works ok
NewFileCreated( this, new FileCreatedEventArgs() { Template = Template.BBMF, FilePath = saveFileDialog.FileName } );
// Second Event clears file content as soon as saveFileDialog.OpenFile() called
FileLoaded( this, new FileLoadedEventArgs() { FileStream = saveFileDialog.OpenFile() } );
}
有人可以解释为什么会发生这种情况以及我需要做什么才能成功打开新创建的文件?