/// <summary> /// Parses the metadata of the given <paramref name="files"/>. /// </summary> /// <param name="files">Files whose metadata to parse.</param> /// <returns>Task.</returns> private async Task ParseFiles(string[] files) { OnStatusUpdated("Trying to parse selected files..."); List <string> errors = new List <string>(); var newFiles = new List <LoadedFileViewModel>(); await Task.Run(() => { foreach (string file in files) { try { if (SUPPORTEDFILES.Contains(Path.GetExtension(file).ToLower())) { ILocalFile audioFile = _localFileFactory.CreateFile(file); if (string.IsNullOrEmpty(audioFile.Artist)) { throw new Exception("No artist name found"); } if (string.IsNullOrEmpty(audioFile.Track)) { throw new Exception("No track name found"); } LoadedFileViewModel vm = new LoadedFileViewModel(audioFile); vm.ToScrobbleChanged += ToScrobbleChanged; newFiles.Add(vm); } } catch (Exception ex) { errors.Add(file + " " + ex.Message); } } }); LoadedFiles = new ObservableCollection <LoadedFileViewModel>(LoadedFiles.Concat(newFiles)); if (errors.Count > 0) { OnStatusUpdated("Finished parsing selected files. " + errors.Count + " files could not be parsed"); if (_windowManager.MessageBoxService.ShowDialog("Some files could not be parsed. Do you want to save a text file with the files that could not be parsed?", "Error parsing files", IMessageBoxServiceButtons.YesNo) == IMessageBoxServiceResult.Yes) { IFileDialog sfd = _windowManager.CreateSaveFileDialog(); sfd.Filter = "Text Files|*.txt"; if (sfd.ShowDialog()) { File.WriteAllLines(sfd.FileName, errors.ToArray()); } } } else { OnStatusUpdated("Successfully parsed selected files. Parsed " + LoadedFiles.Count + " files"); } }
/// <summary> /// Parses the metadata of the given <paramref name="files"/>. /// </summary> /// <param name="files">Files whose metadata to parse.</param> /// <returns>Task.</returns> private async Task ParseFiles(string[] files) { OnStatusUpdated("Trying to parse selected files..."); List <string> errors = new List <string>(); await Task.Run(() => { foreach (string file in files) { try { if (SUPPORTEDFILES.Contains(Path.GetExtension(file).ToLower())) { TagLib.File audioFile = TagLib.File.Create(file); if (audioFile.Tag.FirstPerformer == null || audioFile.Tag.Title == null) { throw new Exception("No artist name or track title found!"); } LoadedFileViewModel vm = new LoadedFileViewModel(audioFile); vm.ToScrobbleChanged += ToScrobbleChanged; _dispatcher.Invoke(() => LoadedFiles.Add(vm)); } } catch (Exception ex) { errors.Add(file + " " + ex.Message); } } }); if (errors.Count > 0) { OnStatusUpdated("Finished parsing selected files. " + errors.Count + " files could not be parsed"); if (MessageBox.Show("Some files could not be parsed. Do you want to save a text file with the files that could not be parsed?", "Error parsing files", MessageBoxButtons.YesNo) == DialogResult.Yes) { SaveFileDialog sfd = new SaveFileDialog() { Filter = "Text Files|*.txt" }; if (sfd.ShowDialog() == DialogResult.OK) { File.WriteAllLines(sfd.FileName, errors.ToArray()); } } } else { OnStatusUpdated("Successfully parsed selected files. Parsed " + LoadedFiles.Count + " files"); } }
/// <summary> /// Shows a dialog to select music files. /// Parses the selected files. /// </summary> public async void AddFiles() { EnableControls = false; OpenFileDialog ofd = new OpenFileDialog(); ofd.Multiselect = true; ofd.Filter = "Music Files|*.mp3;*.m4a;*.wma"; if (ofd.ShowDialog() == DialogResult.OK) { OnStatusUpdated("Trying to parse selected files..."); List<string> errors = new List<string>(); await Task.Run(() => { foreach (string file in ofd.FileNames) { try { File audioFile = File.Create(file); if (audioFile.Tag.FirstPerformer == null || audioFile.Tag.Title == null) throw new Exception("No artist name or track title found!"); LoadedFileViewModel vm = new LoadedFileViewModel(audioFile); vm.ToScrobbleChanged += ToScrobbleChanged; vm.IsSelectedChanged += IsSelectedChanged; _dispatcher.Invoke(() => LoadedFiles.Add(vm)); } catch (Exception ex) { errors.Add(file + " " + ex.Message); } } }); if (errors.Count > 0) { OnStatusUpdated("Finished parsing selected files. " + errors.Count + " files could not be parsed"); if (MessageBox.Show("Some files could not be parsed. Do you want to save a text file with the files that could not be parsed?", "Error parsing files", MessageBoxButtons.YesNo) == DialogResult.Yes) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Text Files|*.txt"; if (sfd.ShowDialog() == DialogResult.OK) System.IO.File.WriteAllLines(sfd.FileName, errors.ToArray()); } } else OnStatusUpdated("Successfully parsed selected files. Parsed " + LoadedFiles.Count + " files"); } EnableControls = true; }