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();
        }
    }
}
Macklin
  • 146
  • 1
  • 1
  • 8
  • 4
    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 [mcve] so that we can be sure we're not missing anything? – Lasse V. Karlsen Dec 03 '18 at 14:37
  • "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. – Hans Passant Dec 03 '18 at 14:51
  • @HansPassant That expectedly prevents the overwrite prompt from showing altogether. (referring to previous comment). I will test as you've suggested and report back. – Macklin Dec 03 '18 at 14:54
  • @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. – Macklin Dec 03 '18 at 15:03
  • @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. – glopes Jan 20 '19 at 22:25

1 Answers1

7

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

Peter S.
  • 144
  • 1
  • 7
  • 3
    This .NET bug is fixed by KB4481031 (https://support.microsoft.com/en-us/help/4481031/january-22-2019-kb4481031). Currently, you have to go to Windows Update and click "Check for updates" to get it to download and install. – Bill Menees Feb 07 '19 at 14:44