Condividi tramite


SaveFileDialog Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Provides a dialog box that enables the user to specify options for saving a file.

Inheritance Hierarchy

System.Object
  System.Windows.Controls.SaveFileDialog

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

Syntax

'Declaration
Public NotInheritable Class SaveFileDialog
public sealed class SaveFileDialog

The SaveFileDialog type exposes the following members.

Constructors

  Name Description
Public method SaveFileDialog Initializes a new instance of the SaveFileDialog class.

Top

Properties

  Name Description
Public property DefaultExt Gets or sets the default file name extension applied to files that are saved with the SaveFileDialog.
Public property DefaultFileName Gets or sets the file name used if a file name is not specified by the user.
Public property Filter Gets or sets a filter string that specifies the files types and descriptions to display in the SaveFileDialog.
Public property FilterIndex Gets or sets the index of the selected item in the Save as type drop-down list.
Public property SafeFileName Gets the file name for the selected file associated with the SaveFileDialog.

Top

Methods

  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method OpenFile Opens the file specified by the SafeFileName property.
Public method ShowDialog() Displays a SaveFileDialog that is modal to the Web browser or main window.
Public method ShowDialog(Window) Displays an SaveFileDialog that is modal to the specified window.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

This class enables the user to specify a file name and location to save a file.

The following illustration shows a an SaveFileDialog in a Silverlight running on Windows 7 and hosted in Internet Explorer.

Save File Dialog

Save file dialog box

You show a save dialog control using the ShowDialog method. For security purposes Silverlight file and print dialogs must be user-initiated. This means you must show them from a user-initiated action such as the click event handler for a button. In addition, there is a limit on the time allowed between when the user initiates the dialog and when the dialog is shown. If the time limit between these actions is exceeded, an exception will occur.

You can optionally specify a filter for the dialog box by using the Filter property. If no filter is specified, you can set the default file name extension applied to files in the dialog box by using the DefaultExt property.

If you attempt to show the dialog box from KeyDown event handlers and other synchronous calls to application code, such as LayoutUpdated or SizeChanged event handlers, an exception will be thrown. An exception will not be thrown when the application is hosted in Internet Explorer, running in protected mode.

NoteNote:

If you want to save data for an application, you can use isolated storage. For more information, see IsolatedStorageFile.

NoteNote:

The Silverlight plug-in does not support SaveFileDialog in full-screen mode. In most cases, displaying the dialog box in full-screen mode will cause the plug-in to revert to embedded mode. However, to avoid issues on some browsers, you should exit full-screen mode before using these classes.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 StackFrame is not supported in Silverlight for Windows Phone.

Examples

The following code example shows how to use the SaveFileDialog.

Private textDialog As SaveFileDialog
Public Sub New()
    InitializeComponent()
    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.")
        sw.Flush()
        sw.Close()
    End If
End Sub
SaveFileDialog textDialog;
public Page()
{
    InitializeComponent();
    textDialog = new SaveFileDialog();
    textDialog.Filter = "Text Files | *.txt";
    textDialog.DefaultExt = "txt";
 }

private void button1_Click(object sender, RoutedEventArgs e)
{
    bool? result = textDialog.ShowDialog();
    if (result == true)
    {
        System.IO.Stream fileStream = textDialog.OpenFile();
        System.IO.StreamWriter sw = new System.IO.StreamWriter(fileStream);
        sw.WriteLine("Writing some text in the file.");
        sw.Flush();
        sw.Close();
    }
}
<Grid x:Name="LayoutRoot" Background="White">
     <Button Height="100" Width="100" Content="Save Text File" x:Name="button1" 
        Click="button1_Click"  />
</Grid>

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

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

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.