From 18cad9dd737d1ddfdd1d9ca5188c807f6cf168fc Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Mon, 5 Aug 2019 21:58:45 +0200 Subject: [PATCH] Make "Save as solution" logic more similar to "Save as project" logic: * don't propose an initial directory (#1598) * allow overwriting an existing directory --- ILSpy/Commands/SaveCodeContextMenuEntry.cs | 62 +++++++++------------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/ILSpy/Commands/SaveCodeContextMenuEntry.cs b/ILSpy/Commands/SaveCodeContextMenuEntry.cs index 8ee945f0d..ef395f265 100644 --- a/ILSpy/Commands/SaveCodeContextMenuEntry.cs +++ b/ILSpy/Commands/SaveCodeContextMenuEntry.cs @@ -62,8 +62,7 @@ namespace ICSharpCode.ILSpy.TextView if (singleSelection.Save(textView)) return; } else if (selectedNodes.Count > 1 && selectedNodes.All(n => n is AssemblyTreeNode)) { - var initialPath = Path.GetDirectoryName(((AssemblyTreeNode)selectedNodes[0]).LoadedAssembly.FileName); - var selectedPath = SelectSolutionFile(initialPath); + var selectedPath = SelectSolutionFile(); if (!string.IsNullOrEmpty(selectedPath)) { var assemblies = selectedNodes.OfType() @@ -87,45 +86,36 @@ namespace ICSharpCode.ILSpy.TextView /// will be used. /// /// The full path of the selected target file, or null if the user canceled. - static string SelectSolutionFile(string path) + static string SelectSolutionFile() { - const string SolutionExtension = ".sln"; - const string DefaultSolutionName = "Solution"; - - if (string.IsNullOrWhiteSpace(path)) { - path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); - } - SaveFileDialog dlg = new SaveFileDialog(); - dlg.InitialDirectory = path; - dlg.FileName = Path.Combine(path, DefaultSolutionName + SolutionExtension); - dlg.Filter = "Visual Studio Solution file|*" + SolutionExtension; + dlg.FileName = "Solution.sln"; + dlg.Filter = "Visual Studio Solution file|*.sln|All files|*.*"; - bool targetInvalid; - do { - if (dlg.ShowDialog() != true) { - return null; - } + if (dlg.ShowDialog() != true) { + return null; + } - string selectedPath = Path.GetDirectoryName(dlg.FileName); - try { - targetInvalid = Directory.EnumerateFileSystemEntries(selectedPath).Any(); - } catch (Exception e) when (e is IOException || e is UnauthorizedAccessException || e is System.Security.SecurityException) { - MessageBox.Show( - "The directory cannot be accessed. Please ensure it exists and you have sufficient rights to access it.", - "Solution directory not accessible", - MessageBoxButton.OK, MessageBoxImage.Error); - targetInvalid = true; - continue; - } + string selectedPath = Path.GetDirectoryName(dlg.FileName); + bool directoryNotEmpty; + try { + directoryNotEmpty = Directory.EnumerateFileSystemEntries(selectedPath).Any(); + } catch (Exception e) when (e is IOException || e is UnauthorizedAccessException || e is System.Security.SecurityException) { + MessageBox.Show( + "The directory cannot be accessed. Please ensure it exists and you have sufficient rights to access it.", + "Solution directory not accessible", + MessageBoxButton.OK, MessageBoxImage.Error); + return null; + } - if (targetInvalid) { - MessageBox.Show( - "The directory is not empty. Please select an empty directory.", - "Solution directory not empty", - MessageBoxButton.OK, MessageBoxImage.Warning); - } - } while (targetInvalid); + if (directoryNotEmpty) { + var result = MessageBox.Show( + Resources.AssemblySaveCodeDirectoryNotEmpty, + Resources.AssemblySaveCodeDirectoryNotEmptyTitle, + MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No); + if (result == MessageBoxResult.No) + return null; // -> abort + } return dlg.FileName; }