Browse Source

Make "Save as solution" logic more similar to "Save as project" logic:

* don't propose an initial directory (#1598)
  * allow overwriting an existing directory
pull/1633/head
Daniel Grunwald 6 years ago
parent
commit
18cad9dd73
  1. 38
      ILSpy/Commands/SaveCodeContextMenuEntry.cs

38
ILSpy/Commands/SaveCodeContextMenuEntry.cs

@ -62,8 +62,7 @@ namespace ICSharpCode.ILSpy.TextView
if (singleSelection.Save(textView)) if (singleSelection.Save(textView))
return; return;
} else if (selectedNodes.Count > 1 && selectedNodes.All(n => n is AssemblyTreeNode)) { } else if (selectedNodes.Count > 1 && selectedNodes.All(n => n is AssemblyTreeNode)) {
var initialPath = Path.GetDirectoryName(((AssemblyTreeNode)selectedNodes[0]).LoadedAssembly.FileName); var selectedPath = SelectSolutionFile();
var selectedPath = SelectSolutionFile(initialPath);
if (!string.IsNullOrEmpty(selectedPath)) { if (!string.IsNullOrEmpty(selectedPath)) {
var assemblies = selectedNodes.OfType<AssemblyTreeNode>() var assemblies = selectedNodes.OfType<AssemblyTreeNode>()
@ -87,45 +86,36 @@ namespace ICSharpCode.ILSpy.TextView
/// will be used.</param> /// will be used.</param>
/// ///
/// <returns>The full path of the selected target file, or <c>null</c> if the user canceled.</returns> /// <returns>The full path of the selected target file, or <c>null</c> if the user canceled.</returns>
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(); SaveFileDialog dlg = new SaveFileDialog();
dlg.InitialDirectory = path; dlg.FileName = "Solution.sln";
dlg.FileName = Path.Combine(path, DefaultSolutionName + SolutionExtension); dlg.Filter = "Visual Studio Solution file|*.sln|All files|*.*";
dlg.Filter = "Visual Studio Solution file|*" + SolutionExtension;
bool targetInvalid;
do {
if (dlg.ShowDialog() != true) { if (dlg.ShowDialog() != true) {
return null; return null;
} }
string selectedPath = Path.GetDirectoryName(dlg.FileName); string selectedPath = Path.GetDirectoryName(dlg.FileName);
bool directoryNotEmpty;
try { try {
targetInvalid = Directory.EnumerateFileSystemEntries(selectedPath).Any(); directoryNotEmpty = Directory.EnumerateFileSystemEntries(selectedPath).Any();
} catch (Exception e) when (e is IOException || e is UnauthorizedAccessException || e is System.Security.SecurityException) { } catch (Exception e) when (e is IOException || e is UnauthorizedAccessException || e is System.Security.SecurityException) {
MessageBox.Show( MessageBox.Show(
"The directory cannot be accessed. Please ensure it exists and you have sufficient rights to access it.", "The directory cannot be accessed. Please ensure it exists and you have sufficient rights to access it.",
"Solution directory not accessible", "Solution directory not accessible",
MessageBoxButton.OK, MessageBoxImage.Error); MessageBoxButton.OK, MessageBoxImage.Error);
targetInvalid = true; return null;
continue;
} }
if (targetInvalid) { if (directoryNotEmpty) {
MessageBox.Show( var result = MessageBox.Show(
"The directory is not empty. Please select an empty directory.", Resources.AssemblySaveCodeDirectoryNotEmpty,
"Solution directory not empty", Resources.AssemblySaveCodeDirectoryNotEmptyTitle,
MessageBoxButton.OK, MessageBoxImage.Warning); MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
if (result == MessageBoxResult.No)
return null; // -> abort
} }
} while (targetInvalid);
return dlg.FileName; return dlg.FileName;
} }

Loading…
Cancel
Save