共用方式為


SaveFileDialog.OpenFile 方法

定義

開啟含使用者選取的讀取/寫入使用權限的檔案。

public:
 System::IO::Stream ^ OpenFile();
public System.IO.Stream OpenFile ();
member this.OpenFile : unit -> System.IO.Stream
Public Function OpenFile () As Stream

傳回

Stream

由使用者選取的讀取/寫入檔案。

範例

下列程式碼範例說明如何 SaveFileDialog 建立 、設定成員、使用 ShowDialog 方法呼叫對話方塊,以及開啟選取的檔案。 此範例需要具有放置按鈕的表單。

private:
   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      Stream^ myStream;
      SaveFileDialog^ saveFileDialog1 = gcnew SaveFileDialog;
      saveFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
      saveFileDialog1->FilterIndex = 2;
      saveFileDialog1->RestoreDirectory = true;
      if ( saveFileDialog1->ShowDialog() == ::DialogResult::OK )
      {
         if ( (myStream = saveFileDialog1->OpenFile()) != nullptr )
         {
            
            // Code to write the stream goes here.
            myStream->Close();
         }
      }
   }
private void button1_Click(object sender, System.EventArgs e)
 {
     Stream myStream ;
     SaveFileDialog saveFileDialog1 = new SaveFileDialog();
 
     saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"  ;
     saveFileDialog1.FilterIndex = 2 ;
     saveFileDialog1.RestoreDirectory = true ;
 
     if(saveFileDialog1.ShowDialog() == DialogResult.OK)
     {
         if((myStream = saveFileDialog1.OpenFile()) != null)
         {
             // Code to write the stream goes here.
             myStream.Close();
         }
     }
 }
Private Sub button1_Click(sender As Object, e As System.EventArgs)
    Dim myStream As Stream
    Dim saveFileDialog1 As New SaveFileDialog()
    
    saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    saveFileDialog1.FilterIndex = 2
    saveFileDialog1.RestoreDirectory = True
    
    If saveFileDialog1.ShowDialog() = DialogResult.OK Then
        myStream = saveFileDialog1.OpenFile()
        If (myStream IsNot Nothing) Then
            ' Code to write the stream goes here.
            myStream.Close()
        End If
    End If
End Sub

備註

警告

基於安全性目的,這個方法會建立具有選取名稱的新檔案,並以讀取/寫入權限開啟它。 如果您選取要儲存的現有檔案,這可能會導致意外遺失資料。 若要在保留現有資料時將資料儲存到現有的檔案,請使用 類別,使用 File 屬性中傳回的 FileName 檔案名來開啟檔案。

適用於

另請參閱