C# Prompt to warn user of overwriting .txt file
<p>Not sure how to implement this, i am not using SaveFileDialog which i have seen uses OverWritePrompt = true cant seem to get that to work for me.</p>

<p>I am using WPF.</p>

<p>The structure:-</p>

<p>I have a textBox called filePathBox - This contains a file path used from opening an: OpenFileDialog</p>

<pre><code>private void fileBrowser_Click(object sender, RoutedEventArgs e)
        {

            //Firstly creating the OpenFileDialog
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            //Setting the filter for the file extension of the files as well as the default extension
            dlg.DefaultExt = ".txt";
            dlg.Filter = "All Files|*.*";

            //Display the dialog box by calling the ShowDialog method
            Nullable&lt;bool&gt; result = dlg.ShowDialog();

            //Grab the file you selected and display it in filePathBox
            if (result == true)
            {
                //Open The document
                string filename = dlg.FileName;
                filePathBox.Text = filename;
            }
        }
</code></pre>

<p>You can then click a button and the .txt file displays in a textBox called textResult</p>

<pre><code>private void helpfulNotes_Click(object sender, RoutedEventArgs e)
        {


            if (File.Exists(filePathBox.Text) &amp;&amp; System.IO.Path.GetExtension(filePathBox.Text).ToLower() == ".txt")
            {
                textResult.Text = File.ReadAllText(filePathBox.Text);
            }

            if (string.IsNullOrWhiteSpace(filePathBox.Text))
            {
                MessageBox.Show("Please choose a file by clicking on the folder Icon :(");
            }

        }
</code></pre>

<p>Once you have made changes to that text in 'textResult' i have a button to save the text back to the file path that was originally loaded using the OpenFileDialog</p>

<pre><code>private void saveText_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(textResult.Text))
            {
                saveText.IsEnabled = false;
                MessageBox.Show("No Text to save!");
            }
            else
            {
                saveText.IsEnabled = true;

                string test = textResult.Text;
                System.IO.File.WriteAllText(filePathBox.Text, test);
            }

            //fileSaveIcon.Visibility = Visibility.Visible;
            //fileChangedIcon.Visibility = Visibility.Hidden;

        }
</code></pre>

<p>At the moment it all saves fine, only it doesn't prompt the user saying are you sure you want to overwrite the file.</p>

<p>At the moment i could </p>

<ol>
<li>load a file for the purpose of this named TestNote.txt into the
filePathBox</li>
<li>Type some text in textResult before even clicking to display the
file</li>
<li>Click save and it would just overwrite TestNote.txt with the text i
just entered without even warning me</li>
</ol>

<p>Hopefully i have explained this adequately and provided all the code you need</p>