Skip to content

Commit

Permalink
Modify code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcd72 committed Jun 19, 2022
1 parent f357651 commit 78fb7c4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 52 deletions.
Binary file modified img/demo-1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 35 additions & 22 deletions src/EasyDeleteDirectory/EasyDeleteDirectoryForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 37 additions & 30 deletions src/EasyDeleteDirectory/EasyDeleteDirectoryForm.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace EasyDeleteDirectory
Expand All @@ -10,49 +11,55 @@ public partial class EasyDeleteDirectoryForm : Form

private void SelectDirectoryButton_Click(object sender, EventArgs e)
{
var result = FolderBrowserDialog.ShowDialog();

if (result == DialogResult.OK)
TargetDirectoryPathTextBox.Text = FolderBrowserDialog.SelectedPath;
if (FolderBrowserDialog.ShowDialog() is DialogResult.OK)
TargetDirectoryTextBox.Text = FolderBrowserDialog.SelectedPath;
}

private void ClearDirectoryButton_Click(object sender, EventArgs e) => TargetDirectoryTextBox.Text = string.Empty;

private void DeleteDirectoryButton_Click(object sender, EventArgs e)
{
MessageTextBox.Text = string.Empty;

var selectedDirectoryPath = TargetDirectoryPathTextBox.Text;
var selectedDirectory = TargetDirectoryTextBox.Text;

if (!string.IsNullOrEmpty(selectedDirectoryPath))
if (string.IsNullOrEmpty(selectedDirectory))
{
var subDirectoryPaths =
Directory.GetDirectories(selectedDirectoryPath);
MessageBox.Show("First of all, please select your target directory.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}

var targetDirectoryNames =
TargetDirectoryNamesTextBox.Text.Split(',', StringSplitOptions.RemoveEmptyEntries);
var targetDirectoryNames =
TargetDirectoryNamesTextBox.Text.Split(',', StringSplitOptions.RemoveEmptyEntries);

foreach (var subDirectoryPath in subDirectoryPaths)
{
foreach (var targetDirectoryName in targetDirectoryNames)
{
var targetDirectoryPaths =
Directory.GetDirectories(subDirectoryPath, targetDirectoryName, SearchOption.AllDirectories);

foreach (var targetDirectoryPath in targetDirectoryPaths)
{
MessageTextBox.AppendText($"Find the target directory: {targetDirectoryPath} ... {Environment.NewLine}");

if (Directory.Exists(targetDirectoryPath))
Directory.Delete(targetDirectoryPath, true);
}
}
}

MessageTextBox.AppendText($"Delete all target directory is completed!");
foreach (var subDirectory in Directory.GetDirectories(selectedDirectory))
{
DeleteDirectoryByNames(subDirectory, targetDirectoryNames);
}
else

MessageTextBox.AppendText("Delete all target directory is completed!");
}

private void DeleteDirectoryByNames(string directory, string[] names)
{
if (!names.Any()) return;

foreach (var name in names)
{
MessageBox.Show("First of all, please select your target directory.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
foreach (var subDirectory in Directory.GetDirectories(directory, name, SearchOption.AllDirectories))
{
DeleteDirectory(subDirectory, true, () => MessageTextBox.AppendText($"{subDirectory} deleted! {Environment.NewLine}"));
}
}
}

private static void DeleteDirectory(string directory, bool recursive, Action action)
{
if (!Directory.Exists(directory)) return;

Directory.Delete(directory, recursive);

action();
}
}
}

0 comments on commit 78fb7c4

Please sign in to comment.