Skip to content

Latest commit

 

History

History
179 lines (152 loc) · 7.95 KB

how-to-save-files-using-the-savefiledialog-component.md

File metadata and controls

179 lines (152 loc) · 7.95 KB
title description ms.date dev_langs helpviewer_keywords ms.assetid
How to: Save Files Using the SaveFileDialog Component
Learn how to use the SaveFileDialog component to browse the file system and select files to be saved.
03/30/2017
csharp
vb
cpp
saving files
SaveFileDialog component [Windows Forms], saving files
files [Windows Forms], saving
OpenFile method [Windows Forms], saving files with SaveFileDialog component
02e8f409-b83f-4707-babb-e71f6b223d90

How to: Save Files Using the SaveFileDialog Component

The xref:System.Windows.Forms.SaveFileDialog component allows users to browse the file system and select files to be saved. The dialog box returns the path and name of the file the user has selected in the dialog box. However, you must write the code to actually write the files to disk.

To save a file using the SaveFileDialog component

  • Display the Save File dialog box and call a method to save the file selected by the user.

    Use the xref:System.Windows.Forms.SaveFileDialog component's xref:System.Windows.Forms.SaveFileDialog.OpenFile%2A method to save the file. This method gives you a xref:System.IO.Stream object you can write to.

    The example below uses the xref:System.Windows.Forms.DialogResult property to get the name of the file, and the xref:System.Windows.Forms.OpenFileDialog.OpenFile%2A method to save the file. The xref:System.Windows.Forms.SaveFileDialog.OpenFile%2A method gives you a stream to write the file to.

    In the example below, there is a xref:System.Windows.Forms.Button control with an image assigned to it. When you click the button, a xref:System.Windows.Forms.SaveFileDialog component is instantiated with a filter that allows files of type .gif, .jpeg, and .bmp. If a file of this type is selected in the Save File dialog box, the button's image is saved.

    [!IMPORTANT] To get or set the xref:System.Windows.Forms.FileDialog.FileName%2A property, your assembly requires a privilege level granted by the xref:System.Security.Permissions.FileIOPermission?displayProperty=nameWithType class. If you are running in a partial-trust context, the process might throw an exception due to insufficient privileges. For more information, see Code Access Security Basics.

    The example assumes your form has a xref:System.Windows.Forms.Button control with its xref:System.Windows.Forms.ButtonBase.Image%2A property set to a file of type .gif, .jpeg, or .bmp.

    [!NOTE] The xref:System.Windows.Forms.FileDialog class's xref:System.Windows.Forms.FileDialog.FilterIndex%2A property (which, due to inheritance, is part of the xref:System.Windows.Forms.SaveFileDialog class) uses a one-based index. This is important if you are writing code to save data in a specific format (for example, saving a file in plain text versus binary format). This property is featured in the example below.

    Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
        ' Displays a SaveFileDialog so the user can save the Image
        ' assigned to Button2.
        Dim saveFileDialog1 As 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 <> "" Then
          ' Saves the Image via a FileStream created by the OpenFile method.
          Dim fs As System.IO.FileStream = Ctype _
              (saveFileDialog1.OpenFile(), System.IO.FileStream)
          ' 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.
          Select Case saveFileDialog1.FilterIndex
              Case 1
                Me.button2.Image.Save(fs, _
                    System.Drawing.Imaging.ImageFormat.Jpeg)
    
              Case 2
                Me.button2.Image.Save(fs, _
                    System.Drawing.Imaging.ImageFormat.Bmp)
    
              Case 3
                Me.button2.Image.Save(fs, _
                    System.Drawing.Imaging.ImageFormat.Gif)
            End Select
    
            fs.Close()
        End If
    End Sub
    private void button2_Click(object sender, System.EventArgs e)
    {
        // Displays a SaveFileDialog so the user can save the Image
        // assigned to Button2.
        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();
        }
    }
    private:
        System::Void button2_Click(System::Object ^ sender,
          System::EventArgs ^ e)
        {
          // Displays a SaveFileDialog so the user can save the Image
          // assigned to Button2.
          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();
          }
        }

    (Visual C# and Visual C++) Place the following code in the form's constructor to register the event handler.

    this.button2.Click += new System.EventHandler(this.button2_Click);
    this->button2->Click += gcnew
        System::EventHandler(this, &Form1::button2_Click);

    For more information about writing file streams, see xref:System.IO.FileStream.BeginWrite%2A and xref:System.IO.FileStream.Write%2A.

    [!NOTE] Certain controls, such as the xref:System.Windows.Forms.RichTextBox control, have the ability to save files.

See also