4

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?

Carl
  • 1,224
  • 2
  • 19
  • 35
  • 3
    I'd think it has more to do with the path of the filename than with the file name itself. – Rotem Nov 19 '13 at 14:38
  • Try adding a @"\ in front. FileName = @"\My File.xslx" – DotNetRussell Nov 19 '13 at 14:40
  • @Rotem, good point. The first thing I would check is if you're combining path & filename correctly. I'd use `Path.Combine` – DaveDev Nov 19 '13 at 14:40
  • I didn't provide any path, simply the file name (the prompt always opens in the folder that was selected the last time it was used, which is probably a default behavior). Should this change anything? – Carl Nov 19 '13 at 14:45
  • Why doesn't your filter and file name ext doesn't match? – Yogee Nov 19 '13 at 14:47
  • @Yogee It was simply a typo when I wrote the question, I edited it so it matches. – Carl Nov 19 '13 at 14:51
  • Something is wrong in that specific client's machine, what is his exact OS? – Shadow The GPT Wizard Nov 19 '13 at 14:54
  • 1
    If the default path was say "Control Panel" then it might be a problem.. – stuartd Nov 19 '13 at 14:56
  • @ShadowWizard I asked hime but I didn't get an answer yet. I'll update my question as soon as I have the details. Thanks. – Carl Nov 19 '13 at 14:57
  • @stuartd Good point, I will check to see if it fails when doing so. – Carl Nov 19 '13 at 14:57
  • @stuartd If the path that was selected in the previous session is no longer valid, it defaults to My Documents (without giving any error). – Carl Nov 19 '13 at 15:03

1 Answers1

3

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.

Carl
  • 1,224
  • 2
  • 19
  • 35