Dolphin
Dolphin

Reputation: 138

How to save the file using save file dialog box

I want to save any type of file using save file dialog box... My requirement is based upon the selection of list box(it contain various type of file like .txt,.xls) i want to provide download option using save file dialog box...if user got select .txt file the file store in text format based on the file extension i want to store file...Those file i want to save same to same file copy into the particular location

pl z help me

Dim digresult As DialogResult = MessageBox.Show("Do you want to download ? ", "View", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    If digresult = Windows.Forms.DialogResult.Yes Then
           downlddialog.Filter = "All files (*.*)|*.*"
           downlddialog.Title = "Save a file"
           downlddialog.RestoreDirectory = True
           downlddialog.OverwritePrompt = True
           downlddialog.ShowDialog()
           Dim dr As String = downlddialog.FileName

Upvotes: 0

Views: 27954

Answers (3)

Keshavdas M
Keshavdas M

Reputation: 702

You can pull out the file extension and then appropriate file writing logic for particular file extension see sample code below,

SaveFileDialog oSaveFileDialog = new SaveFileDialog();
            oSaveFileDialog.Filter = "All files (*.*) | *.*";
            if (oSaveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = oSaveFileDialog.FileName;
                string extesion = Path.GetExtension(fileName);
                switch (extesion)
                {
                    case ".txt"://do something here 
                        break;
                    case ".xls"://do something here 
                        break;
                    default://do something here
                        break;
                }
            }    

Upvotes: 3

Ofir
Ofir

Reputation: 5279

This will do the job...

the filter property is optional - it just if you want the user save a specific file type

VB:

// Displays a SaveFileDialog so the user can save the Image
      SaveFileDialog ^ saveFileDialog1 = new SaveFileDialog();
      saveFileDialog1->Filter = 
         "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
      saveFileDialog1->Title = "Save an Image File";
      saveFileDialog1->ShowDialog();
      // If the file name is not an empty string, open it for saving.
      if(saveFileDialog1->FileName != "")
      {
         // Saves the Image through a FileStream created by
         // the OpenFile method.
         System::IO::FileStream ^ fs = 
            safe_cast<System::IO::FileStream*>(
            saveFileDialog1->OpenFile());
         // Saves the Image in the appropriate ImageFormat based on
         // the file type selected in the dialog box.
         // Note that the FilterIndex property is one based.
         switch(saveFileDialog1->FilterIndex)
         {
            case 1 :
               this->button2->Image->Save(fs,
                  System::Drawing::Imaging::ImageFormat::Jpeg);
               break;
            case 2 :
               this->button2->Image->Save(fs, 
                  System::Drawing::Imaging::ImageFormat::Bmp);
               break;
            case 3 :
               this->button2->Image->Save(fs, 
                  System::Drawing::Imaging::ImageFormat::Gif);
               break;
         }
      fs->Close();
      }

C#

// Displays a SaveFileDialog so the user can save the Image
   SaveFileDialog saveFileDialog1 = new SaveFileDialog();
   saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
   saveFileDialog1.Title = "Save an Image File";
   saveFileDialog1.ShowDialog();

   // If the file name is not an empty string open it for saving.
   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 : 
         this.button2.Image.Save(fs, 
            System.Drawing.Imaging.ImageFormat.Jpeg);
         break;

         case 2 : 
         this.button2.Image.Save(fs, 
            System.Drawing.Imaging.ImageFormat.Bmp);
         break;

         case 3 : 
         this.button2.Image.Save(fs, 
            System.Drawing.Imaging.ImageFormat.Gif);
         break;
      }

   fs.Close();
   }

Upvotes: 0

Suraj
Suraj

Reputation: 395

        System.Windows.Forms.SaveFileDialog saveFileDialog1;
        saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
        DialogResult dr=  saveFileDialog1.ShowDialog();
        if (dr==DialogResult.OK)
        {
           string filename = saveFileDialog1.FileName;
           //save file using stream.
        }


you can use this code this code is in C# instead of MessageBox.Show use System.Windows.Forms.SaveFileDialog

Upvotes: 3

Related Questions