Reputation: 15413
I'm opening a SaveFileDialog with an initial directory based on a user-defined path. I want make sure this path is valid before passing it in and opening the dialog. Right now I've got 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 \
is slipping by and causing a crash when I call ShowDialog. Are there other values that could cause crashes? What rules does the InitialDirectory property need to follow?
Upvotes: 4
Views: 2278
Reputation: 14561
The quick and easy way to fix it would be to get the full path:
dialog.InitialDirectory = Path.GetFullPath(initialDirectory);
This will expand relative paths to the absolute ones that the SaveFileDialog
expects. This will expand just about anything that resembles a path into a full, rooted path. This includes things like "/" (turns into the root of whatever drive the current folder is set to) and "" (turns into the current folder).
Upvotes: 9