1

这里有点奇怪,我正在编写一个提供保存文件选项的应用程序,保存文件对话框正常编码

SaveFileDialog ofd = new SaveFileDialog();

对话框出现没有问题,单击保存不会引发任何错误,但是没有保存文件,我不知道为什么,有什么想法吗?我已经google了,找不到类似的问题

4

2 回答 2

3

SaveFileDialog类不保存任何内容,它提示用户选择位置和文件名来保存文件。保存文件是你的工作

从上面的 MSDN 链接中提取的这个例子解释了这个概念

private void button1_Click(object sender, System.EventArgs e)
{
     Stream myStream ;
     SaveFileDialog saveFileDialog1 = new SaveFileDialog();

     saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"  ;
     saveFileDialog1.FilterIndex = 2 ;
     saveFileDialog1.RestoreDirectory = true ;

     if(saveFileDialog1.ShowDialog() == DialogResult.OK)
     {
         if((myStream = saveFileDialog1.OpenFile()) != null)
         {
             // Code to write the stream goes here.
             myStream.Close();
         }
     }
}
于 2013-07-31T10:54:16.080 回答
0
Stream stream;
ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"  ;
     ofd.FilterIndex = 2 ;
     ofd.RestoreDirectory = true ;

if(ofd.ShowDialog() == DialogResult.OK)
     {
         if((stream = ofd.OpenFile()) != null)
         {
    //FileStream might be better for you but since i don't know what you write, this will serve as an example
             stream.Write(bytes,offset,count);
             stream.Close();
         }
于 2013-07-31T11:02:27.493 回答