private async void LibraryCleaner_ContextMenuItem_Click(object sender, RoutedEventArgs e) { try { if (LibraryCleaner.SelectedItems.Count == 0) { return; } foreach (var junk in LibraryCleaner.SelectedItems.OfType <Definitions.List.JunkInfo>().ToList()) { switch ((string)(sender as MenuItem)?.Tag) { default: case "Explorer": junk.FSInfo.Refresh(); if (junk.FSInfo.Exists) { Process.Start(junk.FSInfo.FullName); } break; case "Delete": junk.FSInfo.Refresh(); if (junk.FSInfo is FileInfo) { if (junk.FSInfo.Exists) { File.SetAttributes(junk.FSInfo.FullName, FileAttributes.Normal); await Task.Run(() => junk.FSInfo.Delete()).ConfigureAwait(true); } } else { if (((DirectoryInfo)junk.FSInfo).Exists) { await Task.Run(() => ((DirectoryInfo)junk.FSInfo).Delete(true)).ConfigureAwait(true); } } Definitions.List.JunkItems.Remove(junk); break; case "Ignore": Definitions.List.IgnoredJunkItems.Add(junk.FSInfo.FullName); Definitions.List.JunkItems.Remove(junk); break; } } } catch (Exception ex) { Logger.Fatal(ex); } }
// Library Cleaner Button actions private async void LibraryCleaner_ButtonClick(object sender, RoutedEventArgs e) { try { if (LibraryCleaner.Items.Count == 0) { return; } switch ((string)(sender as Button)?.Tag) { case "MoveAll": { var targetFolderBrowser = new System.Windows.Forms.FolderBrowserDialog(); var targetFolderDialogResult = targetFolderBrowser.ShowDialog(); if (targetFolderDialogResult != System.Windows.Forms.DialogResult.OK) { return; } if (Directory.GetDirectoryRoot(targetFolderBrowser.SelectedPath) == targetFolderBrowser.SelectedPath && await Main.FormAccessor.ShowMessageAsync(Functions.SLM.Translate(nameof(Properties.Resources.Forms_LibraryCleaner_RootPathSelected)), Functions.SLM.Translate(nameof(Properties.Resources.Forms_LibraryCleaner_RootPathSelectedMessage)), MessageDialogStyle.AffirmativeAndNegative).ConfigureAwait(true) != MessageDialogResult.Affirmative) { return; } var progressInformationMessage = await Main.FormAccessor.ShowProgressAsync(Functions.SLM.Translate(nameof(Properties.Resources.PleaseWait)), Functions.SLM.Translate(nameof(Properties.Resources.Forms_LibraryCleaner_MovingFiles))).ConfigureAwait(true); progressInformationMessage.SetIndeterminate(); foreach (var junk in LibraryCleaner.ItemsSource.OfType <Definitions.List.JunkInfo>().ToList()) { if (junk.FSInfo is FileInfo) { junk.FSInfo.Refresh(); if (junk.FSInfo.Exists) { progressInformationMessage.SetMessage(Framework.StringFormat.Format(Functions.SLM.Translate(nameof(Properties.Resources.Forms_LibraryCleaner_MovingFile)), new { FileFullName = junk.FSInfo.FullName })); ((FileInfo)junk.FSInfo).CopyTo(Alphaleonis.Win32.Filesystem.Path.Combine(targetFolderBrowser.SelectedPath, junk.FSInfo.Name), true); } File.SetAttributes(junk.FSInfo.FullName, FileAttributes.Normal); await Task.Run(() => junk.FSInfo.Delete()).ConfigureAwait(true); } else { junk.FSInfo.Refresh(); if (junk.FSInfo.Exists) { foreach (FileInfo currentFile in ((DirectoryInfo)junk.FSInfo).EnumerateFileSystemInfos("*", SearchOption.AllDirectories).Where(x => x is FileInfo).ToList()) { var newFile = new FileInfo(currentFile.FullName.Replace(junk.Library.DirectoryList["SteamApps"].FullName, targetFolderBrowser.SelectedPath)); if (!newFile.Exists || (newFile.Length != currentFile.Length || newFile.LastWriteTime != currentFile.LastWriteTime)) { if (!newFile.Directory.Exists) { newFile.Directory.Create(); } progressInformationMessage.SetMessage(Framework.StringFormat.Format(Functions.SLM.Translate(nameof(Properties.Resources.Forms_LibraryCleaner_MovingFile)), new { FileFullName = currentFile.FullName })); await Task.Run(() => currentFile.CopyTo(newFile.FullName, true)).ConfigureAwait(true); } } progressInformationMessage.SetMessage(Framework.StringFormat.Format(Functions.SLM.Translate(nameof(Properties.Resources.Forms_LibraryCleaner_DeletingDirectory)), new { DirectoryFullPath = junk.FSInfo.FullName })); await Task.Run(() => (junk.FSInfo as DirectoryInfo)?.Delete(true)).ConfigureAwait(true); } } Definitions.List.JunkItems.Remove(junk); } await progressInformationMessage.CloseAsync().ConfigureAwait(true); targetFolderBrowser.Dispose(); break; } case "DeleteAll": { if (await Main.FormAccessor.ShowMessageAsync(Functions.SLM.Translate(nameof(Properties.Resources.Forms_LibraryCleaner_DeleteWarning)), Functions.SLM.Translate(nameof(Properties.Resources.Forms_LibraryCleaner_DeleteWarningMessage)), MessageDialogStyle.AffirmativeAndNegative).ConfigureAwait(true) == MessageDialogResult.Affirmative) { var progressInformationMessage = await Main.FormAccessor.ShowProgressAsync(Functions.SLM.Translate(nameof(Properties.Resources.PleaseWait)), Functions.SLM.Translate(nameof(Properties.Resources.Forms_LibraryCleaner_Delete)), true).ConfigureAwait(true); progressInformationMessage.SetIndeterminate(); foreach (var junk in LibraryCleaner.ItemsSource.OfType <Definitions.List.JunkInfo>().ToList()) { if (junk.FSInfo is FileInfo) { junk.FSInfo.Refresh(); if (junk.FSInfo.Exists) { File.SetAttributes(junk.FSInfo.FullName, FileAttributes.Normal); progressInformationMessage.SetMessage(Framework.StringFormat.Format(Functions.SLM.Translate(nameof(Properties.Resources.Forms_LibraryCleaner_MovingFile)), new { FileFullName = junk.FSInfo.FullName })); await Task.Run(() => junk.FSInfo.Delete()).ConfigureAwait(true); } } else { junk.FSInfo.Refresh(); if (junk.FSInfo.Exists) { progressInformationMessage.SetMessage(Framework.StringFormat.Format(Functions.SLM.Translate(nameof(Properties.Resources.Forms_LibraryCleaner_DeletingDirectory)), new { DirectoryFullPath = junk.FSInfo.FullName })); await Task.Run(() => ((DirectoryInfo)junk.FSInfo).Delete(true)).ConfigureAwait(true); } } Definitions.List.JunkItems.Remove(junk); } await progressInformationMessage.CloseAsync().ConfigureAwait(true); } break; } } } catch (IOException ex) { Logger.Error(ex); if (Main.FormAccessor.IsAnyDialogOpen) { await Main.FormAccessor.LibraryCleanerView.Dispatcher.Invoke(async delegate { await Main.FormAccessor.HideMetroDialogAsync(await Main.FormAccessor.GetCurrentDialogAsync <BaseMetroDialog>().ConfigureAwait(true)).ConfigureAwait(true); }, System.Windows.Threading.DispatcherPriority.Normal).ConfigureAwait(true); } } catch (UnauthorizedAccessException ex) { Logger.Error(ex); if (Main.FormAccessor.IsAnyDialogOpen) { await Main.FormAccessor.LibraryCleanerView.Dispatcher.Invoke(async delegate { await Main.FormAccessor.HideMetroDialogAsync(await Main.FormAccessor.GetCurrentDialogAsync <BaseMetroDialog>().ConfigureAwait(true)).ConfigureAwait(true); }, System.Windows.Threading.DispatcherPriority.Normal).ConfigureAwait(true); } } catch (Exception ex) { Logger.Fatal(ex); if (Main.FormAccessor.IsAnyDialogOpen) { await Main.FormAccessor.LibraryCleanerView.Dispatcher.Invoke(async delegate { await Main.FormAccessor.HideMetroDialogAsync(await Main.FormAccessor.GetCurrentDialogAsync <BaseMetroDialog>().ConfigureAwait(true)).ConfigureAwait(true); }, System.Windows.Threading.DispatcherPriority.Normal).ConfigureAwait(true); } } }
public override void SetAttributes(string path, FileAttributes fileAttributes) { AfsFile.SetAttributes(path, fileAttributes); }