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,66 +7,114 @@ 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)
{ {
string musicFolder;
using (var dialog = new FolderBrowserDialog())
{
var result = dialog.ShowDialog();
if (result != DialogResult.OK)
return;
if (string.IsNullOrWhiteSpace(dialog.SelectedPath))
return;
musicFolder = dialog.SelectedPath;
}
if (!musicFolder.EndsWith("\\") && !musicFolder.EndsWith("/"))
musicFolder += Path.DirectorySeparatorChar;
string[] paths;
again1:
try try
{ {
string musicFolder; paths = Directory.GetFileSystemEntries(musicFolder, "*.mp3", SearchOption.AllDirectories);
using (var dialog = new FolderBrowserDialog())
{
var result = dialog.ShowDialog();
if (result != DialogResult.OK)
return;
if (string.IsNullOrWhiteSpace(dialog.SelectedPath))
return;
musicFolder = dialog.SelectedPath;
}
if (!musicFolder.EndsWith("\\") && !musicFolder.EndsWith("/"))
musicFolder += Path.DirectorySeparatorChar;
var paths = Directory.GetFileSystemEntries(musicFolder, "*.mp3", SearchOption.AllDirectories);
var objects = new List<object>();
foreach (var path in paths)
{
var file = TagLib.File.Create(path);
string relativePath;
if (path.StartsWith(musicFolder))
relativePath = path.Remove(0, musicFolder.Length);
else
relativePath = path;
objects.Add(new {
filename = relativePath,
artist = file.Tag.FirstPerformer,
title = file.Tag.Title,
bpm = string.Format("{0}BPM", file.Tag.BeatsPerMinute)
});
}
objectListView1.SetObjects(objects);
objectListView1.AutoResizeColumns();
button2.Enabled = true;
} }
catch (Exception ex) catch(Exception ex)
{ {
MessageBox.Show(ex.Message); 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)
{
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;
if (path.StartsWith(musicFolder))
relativePath = path.Remove(0, musicFolder.Length);
else
relativePath = path;
newItems.Add(new Item
{
filename = path,
relativeFilename = relativePath,
artist = file.Tag.FirstPerformer,
title = file.Tag.Title,
bpm = string.Format("{0}BPM", file.Tag.BeatsPerMinute)
});
}
items = newItems;
objectListView1.SetObjects(items);
objectListView1.AutoResizeColumns();
button2.Enabled = items.Count > 0;
}
private void button2_Click(object sender, EventArgs e)
{
} }
} }
} }