Export (0) Print
Expand All

SaveFileDialog Class

Represents a common dialog box that allows the user to specify options for saving a file. This class cannot be inherited.

For a list of all members of this type, see SaveFileDialog Members.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.CommonDialog
            System.Windows.Forms.FileDialog
               System.Windows.Forms.SaveFileDialog

[Visual Basic]
NotInheritable Public Class SaveFileDialog
   Inherits FileDialog
[C#]
public sealed class SaveFileDialog : FileDialog
[C++]
public __gc __sealed class SaveFileDialog : public FileDialog
[JScript]
public class SaveFileDialog extends FileDialog

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.

Remarks

This class allows you to open and overwrite an existing file or create a new file.

Most of the functionality for this class is found in the FileDialog class.

.NET Compact Framework Platform Note:  On the Pocket PC, if you do not specify a file name extension, the control appends the extension of the selected type from the dialog box. On Windows CE .NET, the control does not append an extension.

All platforms support the FilterIndex property which returns the index of the selected extension filter.

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

Namespace: System.Windows.Forms

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

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

See Also

SaveFileDialog Members | System.Windows.Forms Namespace | FileDialog | CommonDialog

Show:
© 2014 Microsoft