How do I prevent a SaveFileDialog from prompting twice to replace/overwrite a file?
Asked Answered
P

1

5

How can I stop a System.Windows.Forms.SaveFileDialog from prompting twice to replace a selected file, and instead prompt only once?

Either I'm missing something, there's something wrong with my install, or the default behaviour is just dumb.

var saveFileDialog = new SaveFileDialog();
saveFileDialog.ShowDialog();
// User selects file and clicks "Save" within the dialog

I'm not doing anything special at all, this is in an empty Windows Forms project, targeting .NET Framework 4.7.2.

Edit: Added full Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            var saveFileDialog = new SaveFileDialog();
            saveFileDialog.ShowDialog();
        }
    }
}
Picco answered 3/12, 2018 at 14:36 Comment(5)
That code should only show the question once, are you sure that is all of the code? I tried the code in a small test project and when selecting an existing file I get the overwrite question, once. Can you please post a minimal reproducible example so that we can be sure we're not missing anything?Mortenson
"wrong with my install" is a high probability on a programmer's machine. The dialog loads the shell extensions, programmers tend to have a lot of them and not always well tested. Just verify if this repeats in another program, test both c:\windows\system32\notepad.exe and c:\windows\syswow64\notepad.exe, File > Save As. Find/disable the evildoer with SysInternals' AutoRuns utility.Synecology
@HansPassant That expectedly prevents the overwrite prompt from showing altogether. (referring to previous comment). I will test as you've suggested and report back.Picco
@HansPassant Notepad 32 or 64-bit doesn't exhibit the same behaviour. But I have suspicions that this is an issue limited to this machine, seeing that LasseVågsætherKarlsen could not replicate the problem.Picco
@HansPassant et al, this is most definitely a bug. My machine just upgraded to the latest windows build last night and this behavior started happening on old C# applications that were not even recompiled.Amenable
V
7

This seems to be a bug in the latest .Net version. It does not happen on Windows builds prior to 10.0.17763

Ventris answered 18/12, 2018 at 15:20 Comment(1)
This .NET bug is fixed by KB4481031 (support.microsoft.com/en-us/help/4481031/…). Currently, you have to go to Windows Update and click "Check for updates" to get it to download and install.Dupondius

© 2022 - 2024 — McMap. All rights reserved.