void worker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; if (!Directory.Exists(path)) { MessageBox.Show(this, "Couldn't refresh list of audios, source directory doesn't exist! (\"" + path + "\")", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); worker.CancelAsync(); return; } Console.WriteLine("Refreshing audio list from \"{0}\"", path); var files = from file in Directory.EnumerateFiles(path, "*", searchChildren ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly) where IsAccepted(Path.GetExtension(file)) select file; if (!files.Any()) { Console.WriteLine("Couldn't find any files"); worker.CancelAsync(); return; } int count = files.Count(); int index = 0; foreach (string file in files) { if (worker.CancellationPending) { e.Cancel = true; return; } string extension = Path.GetExtension(file); string fullPath = Path.GetFullPath(file); string name = AudioTags.GetName(fullPath); string source = Extensions.GetPartOfPathAfter(fullPath, Path.GetFileName(path)); string type = GetExtensionName(extension); TimeSpan length = AudioTags.GetLength(fullPath); Log.WriteLine("Name: {0}", name); AudioInfo info = new AudioInfo(index, name, fullPath, type, length); info.PopulateDevices(devices.ToArray()); ListViewItem item = new ListViewItem(new string[] { "", name, length.ToString("%m\\:ss"), type, source }); info.listItem = item; Infos.Add(info); Console.WriteLine("\"{0}\" added ({1}, {2}, {3})", name, index, type, length.ToString("%m\\:ss")); worker.ReportProgress((int)(((double)index / (double)count) * 100d)); if (this.InvokeRequired) { this.Invoke(updateCurrentLabel, fullPath); } else { updateCurrentLabel(fullPath); } index += 1; } Console.WriteLine("Loaded {0} audios", Infos.Count); e.Result = new int[] { index, count }; }
private void RefreshExtensions() { var changes = new List <Tuple <string, string> >(); if (!File.Exists("extensions.txt")) { MessageBox.Show(this, "Cannot load accepted extensions; extensions.txt is missing!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); this.Close(); return; } Log.WriteLine("Loading accepted extensions from \"extensions.txt\""); var lines = File.ReadLines("extensions.txt"); int index = 0; int loaded = 0; foreach (string line in lines) { index += 1; // index is incremented at start to make sure it increments at every iteration if (line.StartsWith("#") || line.Trim() == "") // comments and blank lines are ignored { continue; } string[] args = line.Split(";", StringSplitOptions.RemoveEmptyEntries); if (args.Length < 2) { Log.WriteLine("{0}: invalid amount of arguments", index); continue; } string type = args[0]; string name = args[1]; if (!type.StartsWith(".")) { Log.WriteLine("{0}: {1} isn't a valid type", index, type); continue; } if (changes.Select(t => t.Item1).Contains(type)) { Log.WriteLine("{0}: {1} is already added", index, type); continue; } changes.Add(new Tuple <string, string>(type, name)); Log.WriteLine("Added extension: {0} ({1})", type, name); loaded += 1; } if (loaded == 0) { MessageBox.Show(this, "No extensions loaded!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } extensions.Clear(); extensions.AddRange(changes); AudioTags.UpdateAllowedExtensions(extensions.Select(e => e.Item1).ToArray()); Log.WriteLine("Loaded {0} extensions", loaded); }