0

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
7
  • Do you want to store the file based on user selection file extension right? Commented Feb 12, 2014 at 6:52
  • yes same file copy as original one Commented Feb 12, 2014 at 9:19
  • downlddialog is savedialog or opendialog? Commented Feb 12, 2014 at 9:22
  • savedialog ..means i want to copy same file using savedialog Commented Feb 12, 2014 at 10:03
  • copy to where how to you save another location? Commented Feb 12, 2014 at 10:31

3 Answers 3

3
        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

Sign up to request clarification or add additional context in comments.

1 Comment

You can switch statement on file extension and write case depending upon file extension and write logic for save.
3

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;
                }
            }    

2 Comments

How to make xls copy using save daliog box
In SaveDialog put filename with extension you want for ex. abc.xls. if generic writing is required the simplest way to write data to any file is, string[] data = {"ABC", "XYZ", "PQR"}; File.AppendAllLines(fileName, data);
0

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();
   }

3 Comments

But i want to save txt,xlx,ppt format...so can i save these type of file
Whatever file i select these file will copy in location those location end user select
@Saurabh you can change the filter types or remove the filter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.