Export (0) Print
Expand All

SaveFileDialog.OverwritePrompt Property

Gets or sets a value indicating whether the Save As dialog box displays a warning if the user specifies a file name that already exists.

[Visual Basic]
Public Property OverwritePrompt As Boolean
[C#]
public bool OverwritePrompt {get; set;}
[C++]
public: __property bool get_OverwritePrompt();
public: __property void set_OverwritePrompt(bool);
[JScript]
public function get OverwritePrompt() : Boolean;
public function set OverwritePrompt(Boolean);

Property Value

true if the dialog box prompts the user before overwriting an existing file if the user specifies a file name that already exists; false if the dialog box automatically overwrites the existing file without prompting the user for permission. The default value is true.

Example

[Visual Basic, C#] The following code example demonstrates using the SaveFile and LoadFile methods with streams. It also demonstrates using the FileName, CreatePrompt, OverwritePrompt, and System.Windows.Forms.TextBox.Click members. This is a complete example that is ready to run when you copy it to your project.

[Visual Basic] 
Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form

    Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents RichTextBox2 As System.Windows.Forms.RichTextBox
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog

    Public Sub New()
        MyBase.New()
        Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.RichTextBox2 = New System.Windows.Forms.RichTextBox
        Me.Button2 = New System.Windows.Forms.Button
        Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
        Me.SuspendLayout()
        Me.RichTextBox1.Location = New System.Drawing.Point(24, 64)
        Me.RichTextBox1.Name = "RichTextBox1"
        Me.RichTextBox1.TabIndex = 0
        Me.RichTextBox1.Text = "Type something here."
        Me.Button1.Location = New System.Drawing.Point(96, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(96, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Save To Stream"
        Me.RichTextBox2.Location = New System.Drawing.Point(152, 64)
        Me.RichTextBox2.Name = "RichTextBox2"
        Me.RichTextBox2.TabIndex = 3
        Me.RichTextBox2.Text = "It will be added to the stream and appear here."
        Me.Button2.Location = New System.Drawing.Point(104, 200)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(88, 32)
        Me.Button2.TabIndex = 4
        Me.Button2.Text = "Save Stream To File"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.RichTextBox2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.RichTextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

    Public Shared Sub Main()
        Application.Run(New Form1)
    End Sub


    ' Declare a new memory stream.
    Dim userInput As New System.IO.MemoryStream

    ' Save the content of RichTextBox1 to the memory stream, appending
    'a LineFeed character.  
    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        RichTextBox1.SaveFile(userInput, RichTextBoxStreamType.PlainText)
        userInput.WriteByte(13)

        ' Display the entire contents of the stream,
        ' by setting its position to 0, to RichTextBox2.
        userInput.Position = 0
        RichTextBox2.LoadFile(userInput, RichTextBoxStreamType.PlainText)
    End Sub


    ' Shows the use of a SaveFileDialog to save a MemoryStream to a file.
    Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click

        ' Set the properties on SaveFileDialog1 so the user is 
        ' prompted to create the file if it doesn't exist 
        ' or overwrite the file if it does exist.
        SaveFileDialog1.CreatePrompt = True
        SaveFileDialog1.OverwritePrompt = True

        ' Set the file name to myText.txt, set the type filter
        ' to text files, and set the initial directory to drive C.
        SaveFileDialog1.FileName = "myText"
        SaveFileDialog1.DefaultExt = "txt"
        SaveFileDialog1.Filter = "Text files (*.txt)|*.txt"
        SaveFileDialog1.InitialDirectory = "c:\"

        ' Call ShowDialog and check for a return value of DialogResult.OK,
        ' which indicates that the file was saved. 
        Dim result As DialogResult = SaveFileDialog1.ShowDialog()
        Dim fileStream As System.IO.Stream

        If (result = DialogResult.OK) Then

            ' Open the file, copy the contents of memoryStream to fileStream,
            ' and close fileStream. Set the memoryStream.Position value to 0 to 
            ' copy the entire stream. 
            fileStream = SaveFileDialog1.OpenFile()
            userInput.Position = 0
            userInput.WriteTo(fileStream)
            fileStream.Close()
        End If
    End Sub

End Class

[C#] 
using System.Windows.Forms;



public class Form1:
    System.Windows.Forms.Form

{
    
    internal System.Windows.Forms.RichTextBox RichTextBox1;
    internal System.Windows.Forms.Button Button1;
    internal System.Windows.Forms.RichTextBox RichTextBox2;
    internal System.Windows.Forms.Button Button2;
    internal System.Windows.Forms.SaveFileDialog SaveFileDialog1;

    public Form1() : base()
    {   
        
        this.RichTextBox1 = new System.Windows.Forms.RichTextBox();
        this.Button1 = new System.Windows.Forms.Button();
        this.RichTextBox2 = new System.Windows.Forms.RichTextBox();
        this.Button2 = new System.Windows.Forms.Button();
        this.SaveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
        this.SuspendLayout();
        this.RichTextBox1.Location = new System.Drawing.Point(24, 64);
        this.RichTextBox1.Name = "RichTextBox1";
        this.RichTextBox1.TabIndex = 0;
        this.RichTextBox1.Text = "Type something here.";
        this.Button1.Location = new System.Drawing.Point(96, 16);
        this.Button1.Name = "Button1";
        this.Button1.Size = new System.Drawing.Size(96, 24);
        this.Button1.TabIndex = 1;
        this.Button1.Text = "Save To Stream";
        this.Button1.Click += new System.EventHandler(Button1_Click);
        this.RichTextBox2.Location = new System.Drawing.Point(152, 64);
        this.RichTextBox2.Name = "RichTextBox2";
        this.RichTextBox2.TabIndex = 3;
        this.RichTextBox2.Text = "It will be added to the stream " 
            + "and appear here.";
        this.Button2.Location = new System.Drawing.Point(104, 200);
        this.Button2.Name = "Button2";
        this.Button2.Size = new System.Drawing.Size(88, 32);
        this.Button2.TabIndex = 4;
        this.Button2.Text = "Save Stream To File";
        this.Button2.Click += new System.EventHandler(Button2_Click);
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(this.Button2);
        this.Controls.Add(this.RichTextBox2);
        this.Controls.Add(this.Button1);
        this.Controls.Add(this.RichTextBox1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

    }


    public static void Main()
    {
        Application.Run(new Form1());
    }


    // Declare a new memory stream.
    System.IO.MemoryStream userInput = new System.IO.MemoryStream();

    // Save the content of RichTextBox1 to the memory stream, 
    // appending a LineFeed character.  
    private void Button1_Click(System.Object sender, 
        System.EventArgs e)
    {
        RichTextBox1.SaveFile(userInput, 
            RichTextBoxStreamType.PlainText);
        userInput.WriteByte(13);

        // Display the entire contents of the stream,
        // by setting its position to 0, to RichTextBox2.
        userInput.Position = 0;
        RichTextBox2.LoadFile(userInput, 
            RichTextBoxStreamType.PlainText);
    }

    // Shows the use of a SaveFileDialog to save a MemoryStream to a file.
    private void Button2_Click(System.Object sender, System.EventArgs e)
    {

        // Set the properties on SaveFileDialog1 so the user is 
        // prompted to create the file if it doesn't exist 
        // or overwrite the file if it does exist.
        SaveFileDialog1.CreatePrompt = true;
        SaveFileDialog1.OverwritePrompt = true;

        // Set the file name to myText.txt, set the type filter
        // to text files, and set the initial directory to drive C.
        SaveFileDialog1.FileName = "myText";
        SaveFileDialog1.DefaultExt = "txt";
        SaveFileDialog1.Filter = "Text files (*.txt)|*.txt";
        SaveFileDialog1.InitialDirectory = "c:\\";

        // Call ShowDialog and check for a return value of DialogResult.OK,
        // which indicates that the file was saved. 
        DialogResult result = SaveFileDialog1.ShowDialog();
        System.IO.Stream fileStream;

        if (result == DialogResult.OK)

            // Open the file, copy the contents of memoryStream to fileStream,
            // and close fileStream. Set the memoryStream.Position value to 0 to 
            // copy the entire stream. 
        {
            fileStream = SaveFileDialog1.OpenFile();
            userInput.Position = 0;
            userInput.WriteTo(fileStream);
            fileStream.Close();
        }
    }
}

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic 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

Show:
© 2014 Microsoft