1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using System.Collections.Generic;
- using System.IO;
- namespace TBL.CSharp.Base
- {
- public static partial class Extension
- {
- /// <summary>
- /// 目录削除
- /// </summary>
- public static void DirectoryDelete(this string path)
- {
- if (Directory.Exists(path))
- Directory.Delete(path, true);
- }
- /// <summary>
- /// 目录确保
- /// </summary>
- 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);
- }
- }
- /// <summary>
- /// 移动目录
- /// </summary>
- 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);
- }
- }
- }
|