Export (0) Print
Expand All

SaveFileDialog.OpenFile Method

Opens the file with read/write permission selected by the user.

[Visual Basic]
Public Function OpenFile() As Stream
[C#]
public Stream OpenFile();
[C++]
public: Stream* OpenFile();
[JScript]
public function OpenFile() : Stream;

Return Value

The read/write file selected by the user.

Remarks

CAUTION   For security purposes, this method creates a new file with the selected name and opens it with read/write permissions. This can cause unintentional loss of data if you select an existing file to save to. To save data to an existing file while retaining existing data, use the File class to open the file using the file name returned in the FileName property.

Example

[Visual Basic, C#, C++] The following example illustrates creating a SaveFileDialog, setting members, calling the dialog box using the ShowDialog method, and opening the selected file. The example assumes a form with a button placed on it.

[Visual Basic] 
Protected 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 Not (myStream Is Nothing) Then
            ' Code to write the stream goes here.
            myStream.Close()
        End If
    End If
End Sub


[C#] 
protected 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();
         }
     }
 }


[C++] 
protected:
void button1_Click(Object* /*sender*/, System::EventArgs* /*e*/)
 {
     Stream* myStream ;
     SaveFileDialog* saveFileDialog1 = new SaveFileDialog();
 
     saveFileDialog1->Filter = S"txt files (*.txt)|*.txt|All files (*.*)|*.*"  ;
     saveFileDialog1->FilterIndex = 2 ;
     saveFileDialog1->RestoreDirectory = true ;
 
     if(saveFileDialog1->ShowDialog() == DialogResult::OK)
     {
         if((myStream = saveFileDialog1->OpenFile()) != 0)
         {
             // Code to write the stream goes here.
             myStream->Close();
         }
     }
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

SaveFileDialog Class | SaveFileDialog Members | System.Windows.Forms Namespace | Stream

Show:
© 2014 Microsoft