共用方式為


操作說明:使用 SaveFileDialog 元件儲存檔案

SaveFileDialog 元件可讓使用者瀏覽檔案系統,並選取要儲存的檔案。 對話方塊會傳回使用者在對話方塊中所選取之檔案的路徑和名稱。 不過,您必須撰寫程式碼,以實際將檔案寫入至磁碟。

使用 SaveFileDialog 元件儲存檔案

  • 顯示 [儲存檔案] 對話方塊,並呼叫方法來儲存使用者所選取的檔案。

    使用 SaveFileDialog 元件的 OpenFile 方法來儲存檔案。 此方法會提供您可以寫入的 Stream 物件。

    下列範例會使用 DialogResult 屬性來取得檔案的名稱,以及 OpenFile 方法來儲存檔案。 OpenFile 方法會提供要寫入檔案的資料流。

    在下列範例中,有已指派影像的 Button 控制項。 當您按一下按鈕時,會使用允許檔案類型 .gif、.jpeg 和 .bmp 的篩選來具現化 SaveFileDialog 元件。 如果在 [儲存檔案] 對話方塊中選取這類型的檔案,則會儲存按鈕的影像。

    重要

    若要取得或設定 FileName 屬性,您的組件需要 System.Security.Permissions.FileIOPermission 類別授與的權限等級。 若在部分信任內容中執行,程序可能會因為權限不足而擲回例外狀況。 如需詳細資訊,請參閱 Code Access Security Basics

    範例會假設您的表單有一個 Button 控制項,其 Image 屬性設定為類型為 .gif、.jpeg 或 .bmp 的檔案。

    注意

    FileDialog 類別的 FilterIndex 屬性 (由於繼承,會屬於 SaveFileDialog 類別的一部分) 使用以一為基礎的索引。 如果您要撰寫程式碼,以特定格式來儲存資料 (例如,以純文字與二進位格式儲存檔案),則這十分重要。 下列範例具有這個屬性。

    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# 和 Visual C++) 請將下列程式碼置於表單的建構函式中,以登錄事件處理常式。

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

    如需寫入檔案資料流的詳細資訊,請參閱 BeginWriteWrite

    注意

    某些控制項,例如 RichTextBox 控制項,能夠儲存檔案。

另請參閱