Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Public Property OverwritePrompt As Boolean

Property Value

Type: System.Boolean
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.

The following code example demonstrates using the RichTextBox.SaveFile and RichTextBox.LoadFile methods with streams. It also demonstrates using the FileName, DefaultExt, CreatePrompt, and OverwritePrompt members.

This is a complete example that is ready to run when you copy it to your project.

Imports System
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms

Partial Public Class Form1
    Inherits Form

    Friend WithEvents RichTextBox1 As RichTextBox
    Friend WithEvents Button1 As Button
    Friend WithEvents RichTextBox2 As RichTextBox
    Friend WithEvents Button2 As Button
    Friend WithEvents SaveFileDialog1 As SaveFileDialog

    Public Sub New()
        Me.RichTextBox1 = New RichTextBox
        Me.Button1 = New Button
        Me.RichTextBox2 = New RichTextBox
        Me.Button2 = New Button
        Me.SaveFileDialog1 = New SaveFileDialog
        Me.RichTextBox1.Location = New Point(24, 64)
        Me.RichTextBox1.Name = "RichTextBox1" 
        Me.RichTextBox1.TabIndex = 0
        Me.RichTextBox1.Text = "Type something here." 
        Me.Button1.Location = New Point(96, 16)
        Me.Button1.Name = "Button1" 
        Me.Button1.Size = New Size(96, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Save To Stream" 
        Me.RichTextBox2.Location = New 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 Point(104, 200)
        Me.Button2.Name = "Button2" 
        Me.Button2.Size = New Size(88, 32)
        Me.Button2.TabIndex = 4
        Me.Button2.Text = "Save Stream To File" 
        Me.ClientSize = New Size(292, 266)
        Me.Name = "Form1" 
        Me.Text = "Form1" 

    End Sub 

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

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

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

        ' 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 Object, _
        ByVal e As 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 the  
        ' MyDocuments folder.
        SaveFileDialog1.FileName = "myText" 
        ' DefaultExt is only used when "All files" is selected from  
        ' the filter box and no extension is specified by the user.
        SaveFileDialog1.DefaultExt = "txt"
        SaveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
        SaveFileDialog1.InitialDirectory = _

        ' 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 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
        End If 
    End Sub 

End Class
The following code example demonstrates using the 
RichTextBox.SaveFile and RichTextBox.LoadFile methods with streams.
It also demonstrates using the FileDialog.FileName, 
SaveFileDialog.CreatePrompt, SaveFileDialog.OverwritePrompt, and 
TextBox.Click members. This is a complete example that is ready to run when you copy it to your project.

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft. All rights reserved.