Question

A customer just had this error with our application, which basically happens when calling ShowDialog on a Microsoft.Win32.SaveFileDialog. The complete stack trace is the following:

System.InvalidOperationException: 'My File.xlsx' is not a valid file name. at Microsoft.Win32.SaveFileDialog.RunFileDialog(OPENFILENAME_I ofn) at Microsoft.Win32.FileDialog.RunLegacyDialog(IntPtr hwndOwner) at Microsoft.Win32.FileDialog.RunDialog(IntPtr hwndOwner) at Microsoft.Win32.CommonDialog.ShowDialog(Window owner) at (our code here)

The code that shows the dialog is pretty standard:

var dialog = new SaveFileDialog
    {
        Filter = "Excel files (.xlsx)|*.xlsx",
        FileName = "My File.xlsx",
    };

if (dialog.ShowDialog() == true)
{
    result = dialog.FileName;
}
else
{
    result = null;
}

Our application is a WPF application running on the .NET 4.0 framework. The code works fine on my machine as well as on every other customer's machine, but it throws this error for this particular customer. I tried doing some research about this, but I did not find anything useful as to what might be causing it. The file name does seem perfectly valid. Any ideas?

Was it helpful?

Solution

It seems that this method does not have the same behavior on Windows XP or on later versions of Windows.

The customer was running Windows XP and the SaveFileDialog was being opened with a file name containing a / ("My / File.xlsx"). This caused ShowDialog() to throw the above error before showing the dialog. However, the error message only contained the part of the file name after the / ("File.xlsx" in this example).

On Windows 8, there is no problem calling ShowDialog with a file name containing a /. It will simply prevent you to save without changing the name, since a name with a / is invalid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top