1

Bit of an odd one here , I'm writing an app which gives a save file option , the save file dialog is coded up as normal

SaveFileDialog ofd = new SaveFileDialog();

the dialog box comes up no problem and clicking save doesn't throw up any errors however no file is saved and I'm not sure why , any ideas ? I've googled it and can't find a similar problem

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user2546071
  • 51
  • 1
  • 10

2 Answers2

3

The SaveFileDialog class doesn't save anything, it prompts the user to choose a location and a file name to save the file. It is your job to save the file

This example extracted from the MSDN link above explains the concept

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();
         }
     }
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • I'm trying to save to the file using the code found here : http://stackoverflow.com/questions/6674555/export-gridview-data-into-csv-file/17971742#17971742 But I'm getting an error sayin that RFesponse does not exist in the current context , does anyone know how to solve this – user2546071 Jul 31 '13 at 13:47
  • That code is for an ASP.NET project where Response is the intrinsic object used to return text to the client browser. You are not in the same context – Steve Jul 31 '13 at 13:49
  • Would you know how I can get in the same context ? sorry just I'm a c# noob and googling doesnt seem to be helping – user2546071 Jul 31 '13 at 13:50
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();
         }
Goran Štuc
  • 581
  • 4
  • 15