Is this really desired behavior in SaveFileDialog.FileName for WPF?

241 Views Asked by At

When using SaveFileDialog, I noticed that there is a difference in the behavior between WPF and WindowsForms when the path is too long.

To verify this, create a long folder name (but not more than the allowed 247 chars), for example:

C:\LooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongFolder

Create a WindowsForms project and a button on the Form with a click event:

using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sFile = new SaveFileDialog();
            sFile.FileName = "Test.txt";
            if (sFile.ShowDialog() == DialogResult.Yes)
                Debug.Print(sFile.FileName);
        }
    }
}

Start the program, move to the long folder in the SaveFileDialog, enter a long enough name, like "MyLittleTextFile.txt", and press enter.

sFile.FileName will throw an internal exception and thus Debug.Print will not be executed.

Now create a WPF project and a button on the MainWindow with a click event:

using System.Diagnostics;
using System.Windows;
using Microsoft.Win32;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sFile = new SaveFileDialog();
            sFile.FileName = "Test.txt";
            if (sFile.ShowDialog() == true)
                Debug.Print(sFile.FileName);
        }
    }
}

Again start the program, move to the long folder in the SaveFileDialog, enter a long enough name, like "MyLittleTextFile.txt", and press enter.

sFile.FileName will now still throw an internal exception, but it will simply return to the old value of "Test.txt".

This is surprising at best, because at this point of course I expect the value to be the one I selected - or getting an error when trying to use it. Having the old value doesn't make much sense in my eyes. Anybody knows why it was implemented this way? Is this desired behavior, or is it a bug?

1

There are 1 best solutions below

0
On

This was on Windows 10 (17134.285) targeting FW 4.7.

When entering a long filename...

sFile.FileName Will contain the full UNC path. (see below)

\\?\C:\LooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongFolder\MyLittleTextFile.txt

sFile.SafeFileName Will contain the filename only.

MyLittleTextFile.txt

enter image description here

With WinForms I was unable to force the dialog to accept a long filename, leaving only a shorter filename or cancelling as an option.

No exceptions were thrown.