保存filedialog无法正常工作

时间:2013-07-31 10:51:52

标签: c# savefiledialog

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

SaveFileDialog ofd = new SaveFileDialog();

对话框出现没问题,单击保存不会引发任何错误,但没有保存文件,我不知道为什么,任何想法?我用谷歌搜索它并找不到类似的问题

2 个答案:

答案 0 :(得分: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();
         }
     }
}

答案 1 :(得分: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();
         }