Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace MusicIDv3Renamer
- {
- public partial class mainForm : Form
- {
- public mainForm()
- {
- InitializeComponent();
- }
- private void openFiles()
- {
- if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- musicsPathListBox.Items.AddRange(openFileDialog1.FileNames);
- }
- }
- private void addBtn_Click(object sender, EventArgs e)
- {
- openFiles();
- }
- private String getNameFromIDv3Tags(String path)
- {
- try
- {
- TagLib.File mp3 = TagLib.File.Create(path);
- String artist = String.Join(", ", mp3.Tag.Performers);
- artist = toUtf8(artist);
- String name = mp3.Tag.Title;
- name = toUtf8(name);
- return (artist + " - " + name).Trim();
- }
- catch (Exception e)
- {
- return "ERROR [" + e.Message + "]";
- }
- }
- private void rename(String path, String newFileName)
- {
- try
- {
- String directory = Path.GetDirectoryName(path);
- String ext = (path.Split('.')[(path.Split('.').Length - 1)]);
- String newPath = directory + "\\" + newFileName + "." + ext;
- System.IO.File.Move(path, newPath);
- }
- catch (Exception e)
- {
- try
- {
- logListBox.Items.Add("Не удалось переименовать файл " + Path.GetFileName(path) + " в " + Path.GetFileName(newFileName) + ". [" + e.Message + "]");
- } catch { }
- }
- }
- private void goBtn_Click(object sender, EventArgs e)
- {
- if (musicsPathListBox.Items.Count <= 0)
- {
- MessageBox.Show("Файлы не добавлены :(", "Пустой список", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- goBtn.Enabled = false;
- addBtn.Enabled = false;
- musicsPathListBox.Enabled = false;
- foreach (String mp3 in musicsPathListBox.Items)
- {
- String newSongName = getNameFromIDv3Tags(mp3);
- if (newSongName.StartsWith("ERROR ["))
- {
- try
- {
- logListBox.Items.Add("Не удалось получить теги с файла " + Path.GetFileName(mp3) + ": " + newSongName);
- }
- catch { }
- }
- else
- {
- if (newSongName == "-")
- {
- try
- {
- logListBox.Items.Add("Файл " + Path.GetFileName(mp3) + " не содержит тегов :( Не переименовываю!");
- }
- catch { }
- }
- else
- {
- rename(mp3, newSongName);
- }
- }
- }
- musicsPathListBox.Items.Clear();
- goBtn.Enabled = true;
- addBtn.Enabled = true;
- musicsPathListBox.Enabled = true;
- MessageBox.Show("Вся работа закончена :)", "Ура!", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- private String toUtf8(string unknown)
- {
- if (unknown != null)
- {
- return new string(unknown.ToCharArray().Select(x => ((x + 848) >= 'А' && (x + 848) <= 'ё') ? (char)(x + 848) : x).ToArray());
- }
- else
- {
- return "";
- }
- }
- private void clearLogBtn_Click(object sender, EventArgs e)
- {
- logListBox.Items.Clear();
- }
- private void clearMusicList_Click(object sender, EventArgs e)
- {
- musicsPathListBox.Items.Clear();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement