private async void LoadPathWindowsFromSettings() { var stopwatch = Stopwatch.StartNew(); await SettingsController.LoadDataDir(); await DownloadController.CleanUp(); List <Task> tasks = new List <Task>(); try { foreach (var dir in DirectoryController.Dirs) { pws.Add(dir.FullPath, new PathWindow(dir.FullPath)); var pw = pws[dir.FullPath]; WindowsComponents.Children.Add(pw); tasks.Add(PathWindow.SetInfoLabel(pw, dir)); } await Task.WhenAll(tasks); } catch (Exception ex) { LoggerController.PrintException(ex); } stopwatch.Stop(); Console.WriteLine($"Loading paths from setings finished in {stopwatch.ElapsedMilliseconds}ms"); Instance = this; }
public static List <DirectoryModel> DeserializeObject(string toDeserialize) { if (toDeserialize is null) { return(null); } List <DirectoryModel> list; try { //XmlSerializer xmlSerializer = new XmlSerializer(typeof(DirectoryModel)); using (StringReader textReader = new StringReader(toDeserialize)) { XmlSerializer deserializer = new XmlSerializer(typeof(List <DirectoryModel>), new XmlRootAttribute("path")); list = (List <DirectoryModel>)deserializer.Deserialize(textReader); return(list); } } catch (Exception e) { LoggerController.PrintException(e); } return(null); }
/// <summary> /// Get info about Directory by path /// </summary> /// <param name="path">Path to Directory</param> /// <param name="reset">Should reset Files and Size</param> public async Task GetAsyncInfo() { try { ConcurrentBag <Task> tasks = new ConcurrentBag <Task>(); // Recursive way for each Directory in the path var dirs = Directory.EnumerateDirectories(FullPath).ToList(); foreach (string dir in dirs) { tasks.Add(GetAsyncInfo()); } await Task.WhenAll(tasks); dirs = Directory.EnumerateFiles(this.FullPath, "*").ToList(); Files = dirs.Count(); foreach (string file in dirs) { Size += new FileInfo(file).Length; } } catch (Exception ex) { LoggerController.PrintException(ex); } }
private static void TryMoveFile(string fullPath, string destinationPath) { string fileName = Path.GetFileName(fullPath); destinationPath = GetUniqueName(fullPath, destinationPath); string destinationDirectory = Path.GetDirectoryName(destinationPath); try { // if Destination directory doesn't exist. if (!Directory.Exists(destinationDirectory)) { Directory.CreateDirectory(destinationDirectory); } File.Move(fullPath, destinationPath); LoggerController.Log($"Moved ('{fileName}') file to ('{destinationDirectory}')"); // Update PathWindow MainWindow.PrintStatistic(destinationDirectory); } catch (Exception e) { LoggerController.PrintException(e); } }
public static void PrintStatistic(string filepath) { Console.WriteLine(filepath); Instance.Dispatcher.Invoke(new Action(() => { try { var element = (pws[filepath] is null) ? null : pws[filepath]; PathWindow.Update(element); } catch (Exception ex) { LoggerController.PrintException(ex); } })); }
public static async Task CleanUp() { Console.WriteLine("Watcher is running..."); // Find existing files var files = Directory.EnumerateFiles(SettingsController.DownloadFolder, "*").ToList(); Console.WriteLine($"Found: {files.Count()} files."); foreach (var file in files) { Console.WriteLine(file); try { await MoveFile(file); } catch (Exception ex) { LoggerController.PrintException(ex); } } Console.WriteLine("Watcher is done."); }
public static void OnError(object sender, ErrorEventArgs e) => LoggerController.PrintException(e.GetException());