Improved error handling

This commit is contained in:
Daniel Brunner
2017-07-09 15:21:48 +02:00
parent 5bc5649beb
commit 22d55ebbd4
2 changed files with 96 additions and 47 deletions

View File

@@ -59,6 +59,7 @@
this.button2.TabIndex = 1; this.button2.TabIndex = 1;
this.button2.Text = "Create BPM folder structure"; this.button2.Text = "Create BPM folder structure";
this.button2.UseVisualStyleBackColor = true; this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
// //
// button1 // button1
// //
@@ -91,7 +92,7 @@
// //
// columnFilename // columnFilename
// //
this.columnFilename.AspectName = "filename"; this.columnFilename.AspectName = "relativeFilename";
this.columnFilename.Text = "Filename"; this.columnFilename.Text = "Filename";
// //
// columnArtist // columnArtist

View File

@@ -7,16 +7,26 @@ namespace MusicOrganizer
{ {
public partial class MainForm : Form public partial class MainForm : Form
{ {
struct Item
{
public string filename;
public string relativeFilename;
public string artist;
public string title;
public string bpm;
}
private List<Item> items;
public MainForm() public MainForm()
{ {
InitializeComponent(); InitializeComponent();
objectListView1.AlwaysGroupByColumn = columnBpm; objectListView1.AlwaysGroupByColumn = columnBpm;
items = new List<Item>();
} }
private void button1_Click(object sender, EventArgs e) private void button1_Click(object sender, EventArgs e)
{
try
{ {
string musicFolder; string musicFolder;
@@ -36,13 +46,48 @@ namespace MusicOrganizer
if (!musicFolder.EndsWith("\\") && !musicFolder.EndsWith("/")) if (!musicFolder.EndsWith("\\") && !musicFolder.EndsWith("/"))
musicFolder += Path.DirectorySeparatorChar; musicFolder += Path.DirectorySeparatorChar;
var paths = Directory.GetFileSystemEntries(musicFolder, "*.mp3", SearchOption.AllDirectories); string[] paths;
var objects = new List<object>(); again1:
try
{
paths = Directory.GetFileSystemEntries(musicFolder, "*.mp3", SearchOption.AllDirectories);
}
catch(Exception ex)
{
var result = MessageBox.Show(string.Format("Error occured when collecting files\n\n{0}", ex.Message), "Error occured", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
switch(result)
{
case DialogResult.Retry: goto again1;
case DialogResult.Cancel: return;
default: throw new Exception("Unknown option clicked.");
}
}
var newItems = new List<Item>();
foreach (var path in paths) foreach (var path in paths)
{ {
var file = TagLib.File.Create(path); TagLib.File file;
again2:
try
{
file = TagLib.File.Create(path);
}
catch(Exception ex)
{
var result = MessageBox.Show(string.Format("Error occured when processing file\n\n{0}\n\n{1}", path, ex.Message), "Error occured", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
switch(result)
{
case DialogResult.Abort: return;
case DialogResult.Retry: goto again2;
case DialogResult.Ignore: continue;
default: throw new Exception("Unknown option clicked.");
}
}
string relativePath; string relativePath;
if (path.StartsWith(musicFolder)) if (path.StartsWith(musicFolder))
@@ -50,23 +95,26 @@ namespace MusicOrganizer
else else
relativePath = path; relativePath = path;
objects.Add(new { newItems.Add(new Item
filename = relativePath, {
filename = path,
relativeFilename = relativePath,
artist = file.Tag.FirstPerformer, artist = file.Tag.FirstPerformer,
title = file.Tag.Title, title = file.Tag.Title,
bpm = string.Format("{0}BPM", file.Tag.BeatsPerMinute) bpm = string.Format("{0}BPM", file.Tag.BeatsPerMinute)
}); });
} }
objectListView1.SetObjects(objects); items = newItems;
objectListView1.SetObjects(items);
objectListView1.AutoResizeColumns(); objectListView1.AutoResizeColumns();
button2.Enabled = true; button2.Enabled = items.Count > 0;
} }
catch (Exception ex)
private void button2_Click(object sender, EventArgs e)
{ {
MessageBox.Show(ex.Message);
}
} }
} }
} }