diff --git a/TGServerService/Program.cs b/TGServerService/Program.cs index 5184c9f194..1394802800 100644 --- a/TGServerService/Program.cs +++ b/TGServerService/Program.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; using System.ServiceProcess; +using System.Threading.Tasks; namespace TGServerService { @@ -38,7 +39,7 @@ namespace TGServerService /// The directory to delete /// If , an empty will remain instead of being deleted fully. Incompatible with /// If any files or directories in the root level of match anything in this of s, they won't be deleted. Incompatible with - public static void DeleteDirectory(string path, bool ContentsOnly = false, IList excludeRoot = null) + public static async void DeleteDirectory(string path, bool ContentsOnly = false, IList excludeRoot = null) { var di = new DirectoryInfo(path); if (!di.Exists) @@ -48,7 +49,7 @@ namespace TGServerService excludeRoot[I] = excludeRoot[I].ToLower(); if (CheckDeleteSymlinkDir(di)) return; - NormalizeAndDelete(di, excludeRoot); + await NormalizeAndDelete(di, excludeRoot, false); if (!ContentsOnly) { if (excludeRoot != null && excludeRoot.Count > 0) @@ -78,24 +79,31 @@ namespace TGServerService /// /// of the directory to empty /// Lowercase file and directory names to skip while emptying this level. Not passed forward - static void NormalizeAndDelete(DirectoryInfo dir, IList excludeRoot) + /// If , will be deleted before the function exits + static async Task NormalizeAndDelete(DirectoryInfo dir, IList excludeRoot, bool deleteRoot) { + var tasks = new List { Task.Factory.StartNew(() => + { + foreach (var file in dir.GetFiles()) + { + if (excludeRoot != null && excludeRoot.Contains(file.Name.ToLower())) + continue; + file.Attributes = FileAttributes.Normal; + file.Delete(); + } + }) }; + foreach (var subDir in dir.GetDirectories()) { if (excludeRoot != null && excludeRoot.Contains(subDir.Name.ToLower())) continue; if (CheckDeleteSymlinkDir(subDir)) continue; - NormalizeAndDelete(subDir, null); - subDir.Delete(true); - } - foreach (var file in dir.GetFiles()) - { - if (excludeRoot != null && excludeRoot.Contains(file.Name.ToLower())) - continue; - file.Attributes = FileAttributes.Normal; - file.Delete(); + tasks.Add(NormalizeAndDelete(subDir, null, true)); } + await Task.WhenAll(tasks); + if(deleteRoot) + dir.Delete(true); } /// @@ -105,7 +113,7 @@ namespace TGServerService /// The destination directory /// List of files and directories to ignore while copying /// If no error will be thrown if does not exist - public static void CopyDirectory(string sourceDirName, string destDirName, IList ignore = null, bool ignoreIfNotExists = false) + public static async void CopyDirectory(string sourceDirName, string destDirName, IList ignore = null, bool ignoreIfNotExists = false) { IList realIgnore; if (ignore != null) @@ -116,7 +124,7 @@ namespace TGServerService } else realIgnore = null; - CopyDirectoryImpl(sourceDirName, destDirName, realIgnore, ignoreIfNotExists); + await CopyDirectoryImpl(sourceDirName, destDirName, realIgnore, ignoreIfNotExists); } /// @@ -126,7 +134,7 @@ namespace TGServerService /// The destination directory /// List of lowercase files and directories to ignore while copying /// If no error will be thrown if does not exist - static void CopyDirectoryImpl(string sourceDirName, string destDirName, IList ignore, bool ignoreIfNotExists) { + static async Task CopyDirectoryImpl(string sourceDirName, string destDirName, IList ignore, bool ignoreIfNotExists) { // If the destination directory doesn't exist, create it. if (!Directory.Exists(destDirName)) { @@ -156,14 +164,16 @@ namespace TGServerService file.CopyTo(temppath, true); } + var tasks = new List(); // copy them and their contents to new location. foreach (DirectoryInfo subdir in dirs) { if (ignore != null && ignore.Contains(subdir.Name.ToLower())) continue; string temppath = Path.Combine(destDirName, subdir.Name); - CopyDirectoryImpl(subdir.FullName, temppath, ignore, false); + tasks.Add(CopyDirectoryImpl(subdir.FullName, temppath, ignore, false)); } + await Task.WhenAll(tasks); } ///