Export (0) Print
Expand All

SaveFileDialog.OpenFile Method


Opens the file specified by the SafeFileName property.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)

<SecuritySafeCriticalAttribute> _
Public Function OpenFile As Stream

Return Value

Type: System.IO.Stream
A read-write stream for the file specified by the SafeFileName property.


No file was selected in the dialog box.

You should always check that the ShowDialog method returns true before accessing the stream associated with the file. Failure to do this will create an exception.

The following code example shows how to use the OpenFile method.

Private textDialog As SaveFileDialog
Public Sub New()
    textDialog = New SaveFileDialog()
    textDialog.Filter = "Text Files | *.txt"
    textDialog.DefaultExt = "txt"
End Sub

Private Sub button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim result As System.Nullable(Of Boolean) = textDialog.ShowDialog()
    If result = True Then
        Dim fileStream As System.IO.Stream = textDialog.OpenFile()
        Dim sw As New System.IO.StreamWriter(fileStream)
        sw.WriteLine("Writing some text in the file.")
    End If
End Sub

<Grid x:Name="LayoutRoot" Background="White">
     <Button Height="100" Width="100" Content="Save Text File" x:Name="button1" 
        Click="button1_Click"  />


Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

© 2014 Microsoft