如何使用保存文件对话框保存文件

时间:2014-02-12 05:04:17

标签: c# vb.net

我想使用保存文件对话框保存任何类型的文件... 我的要求是基于列表框的选择(它包含各种类型的文件,如.txt,.xls)我想使用保存文件对话框提供下载选项...如果用户选择.txt文件文件存储在文本中基于文件扩展名的格式我想存储文件...我希望将相同文件副本保存到特定位置的那些文件

pl z帮帮我

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

3 个答案:

答案 0 :(得分:0)

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


您可以使用此代码,此代码在C#中而不是 MessageBox.Show 使用 System.Windows.Forms.SaveFileDialog

答案 1 :(得分:0)

这将完成这项工作......

filter属性是可选的 - 只是您希望用户保存特定的文件类型

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

答案 2 :(得分:0)

您可以提取文件扩展名,然后为特定文件扩展名提供适当的文件写入逻辑,请参阅下面的示例代码,

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