Check InitialDirectory for SaveFileDialog?

I open SaveFileDialog with the source directory based on the user path. I want to make sure that this path is valid before passing it and opening a dialog. Now I have this:

Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog();

if (!string.IsNullOrEmpty(initialDirectory) && Directory.Exists(initialDirectory))
{
    dialog.InitialDirectory = initialDirectory;
}

bool? result = dialog.ShowDialog();

However, it seems to \slip and cause a crash when calling ShowDialog. Are there other values ​​that can cause failures? What rules should the InitialDirectory property follow?

+3
source share
1 answer

A quick and easy way to fix this is to get the full path:

dialog.InitialDirectory = Path.GetFullPath(initialDirectory);

, SaveFileDialog. , , . , "/" ( , ​​ ) "" ( ).

+7

Source: https://habr.com/ru/post/1794956/


All Articles