Пример #1
0
        private void RenomearArquivos()
        {
            if (DiretorioRaiz != null)
            {
                string artista, titulo, album, nome, separador, newPath, numero;

                int count = 0, max;
                max = DiretorioRaiz.GetFiles().Count();


                try
                {
                    foreach (FileInfo file in DiretorioRaiz.GetFiles())
                    {
                        using (var mp3 = new Mp3File(file))
                        {
                            Id3Tag[] tag;
                            try
                            {
                                tag = mp3.GetAllTags();
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }

                            Id3Tag tagReal;

                            nome = "";
                            if (tag.Count() > 0)
                            {
                                tagReal = tag[0];
                                if (ckNumeracao.Checked)
                                {
                                    count++;
                                    numero = count.ToString().PadLeft(max.ToString().Length, '0') + " ";
                                    nome   = tagReal.Track.Value.ToString() != "0" && tagReal.Track.Value.ToString() != "" ? tagReal.Track.Value.ToString().PadLeft(max.ToString().Length, '0') + " " : numero;
                                }

                                separador = cmbSeparador.SelectedItem.ToString() != "" ? cmbSeparador.SelectedItem.ToString() : " - ";

                                artista = ckArtist.Checked ? tagReal.Artists : "";
                                titulo  = ckTitle.Checked ? tagReal.Title : "";
                                album   = ckAlbum.Checked ? tagReal.Album : "";


                                nome += album + (ckAlbum.Checked && ckArtist.Checked && artista != "" ? separador : "") +
                                        artista + (ckArtist.Checked && ckTitle.Checked && titulo != "" ? separador : "") +
                                        titulo + (ckTitle.Checked && txtExtra.Text != "" ? separador : "") +
                                        txtExtra.Text;
                            }
                            else
                            {
                                nome = txtExtra.Text;
                            }

                            newPath = DiretorioRaiz.FullName + "\\" + Util.TratarNomeArquivo(nome) + file.Extension;
                        }

                        if (!File.Exists(newPath))
                        {
                            File.Move(file.FullName, newPath);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }