using System.Collections.Generic;
using System.IO;
namespace TBL.CSharp.Base
{
public static partial class Extension
{
///
/// 目录削除
///
public static void DirectoryDelete(this string path)
{
if (Directory.Exists(path))
Directory.Delete(path, true);
}
///
/// 目录确保
///
public static void DirectoryEnsure(this string path, bool clear = true)
{
if (clear)
{
DirectoryDelete(path);
Directory.CreateDirectory(path);
}
else
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
}
}
///
/// 移动目录
///
public static void DirectoryMove(this string source, string target)
{
var stack = new Stack<(string Source, string Target)>();
stack.Push((source, target));
while (stack.Count > 0)
{
var folders = stack.Pop();
Directory.CreateDirectory(folders.Target);
foreach (var file in Directory.GetFiles(folders.Source, "*.*"))
{
string targetFile = Path.Combine(folders.Target, Path.GetFileName(file));
if (File.Exists(targetFile)) File.Delete(targetFile);
File.Move(file, targetFile);
}
foreach (var folder in Directory.GetDirectories(folders.Source))
{
stack.Push((folder, Path.Combine(folders.Target, Path.GetFileName(folder))));
}
}
Directory.Delete(source, true);
}
}
}