From 975eb97e274749ffb9b0a85bb770af62606edb07 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 23 Feb 2023 12:31:27 -0500 Subject: [PATCH] Be smarter about old paths (fixes #455) --- MPF/ViewModels/MainViewModel.cs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/MPF/ViewModels/MainViewModel.cs b/MPF/ViewModels/MainViewModel.cs index 2428e6d3..ccd7d5ba 100644 --- a/MPF/ViewModels/MainViewModel.cs +++ b/MPF/ViewModels/MainViewModel.cs @@ -977,23 +977,30 @@ namespace MPF.UI.ViewModels string directory = App.Options.DefaultOutputPath; string filename = $"{label}{extension ?? ".bin"}"; - if (directory.EndsWith(label)) - App.Instance.OutputPathTextBox.Text = Path.Combine(directory, filename); - else - App.Instance.OutputPathTextBox.Text = Path.Combine(directory, label, filename); + // If the path ends with the label already + if (directory.EndsWith(label, StringComparison.OrdinalIgnoreCase)) + directory = Path.GetDirectoryName(directory); + + App.Instance.OutputPathTextBox.Text = Path.Combine(directory, label, filename); } // Set the output filename, if we changed drives else if (driveChanged) { string label = drive?.FormattedVolumeLabel ?? systemType.LongName(); + string oldFilename = Path.GetFileNameWithoutExtension(App.Instance.OutputPathTextBox.Text); string directory = Path.GetDirectoryName(App.Instance.OutputPathTextBox.Text); string filename = $"{label}{extension ?? ".bin"}"; - if (directory.EndsWith(label)) - App.Instance.OutputPathTextBox.Text = Path.Combine(directory, filename); - else - App.Instance.OutputPathTextBox.Text = Path.Combine(directory, label, filename); + // If the previous path included the label + if (directory.EndsWith(oldFilename, StringComparison.OrdinalIgnoreCase)) + directory = Path.GetDirectoryName(directory); + + // If the path ends with the label already + if (directory.EndsWith(label, StringComparison.OrdinalIgnoreCase)) + directory = Path.GetDirectoryName(directory); + + App.Instance.OutputPathTextBox.Text = Path.Combine(directory, label, filename); } // Ensure the UI gets updated