向你们所有人问好。我的问题是:
- 我需要搜索包含 MP3 文件的用户选择的文件夹,并检查哪些文件具有或包含指定的标签。如果是,我会将其复制到指定文件夹。我设法做了一些事情,但只是部分地,例如,我设法复制流派“流行”文件,而不是“布鲁斯”文件。
让contains工作是一场彻头彻尾的噩梦,根本无法让它工作。
代码:
try
{
FolderBrowserDialog folder = new FolderBrowserDialog();
DialogResult result = folder.ShowDialog();
string genre = "Blues";
var matchingFiles = Directory.GetFiles(folder.SelectedPath, "*.mp3", SearchOption.AllDirectories).Where(x =>
{
var f = TagLib.File.Create(x);
return (((TagLib.Id3v2.Tag)f.GetTag(TagTypes.Id3v2)).JoinedGenres == genre);
});
foreach (string f in matchingFiles)
{
System.IO.File.Copy(f, Path.Combine(@"destinationFolder", new FileInfo(f).Name));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
欢迎任何帮助。
问题解决了:
try
{
FolderBrowserDialog folder = new FolderBrowserDialog();
DialogResult result = folder.ShowDialog();
string upit = search.Text;
var matchingFiles = Directory.GetFiles(folder.SelectedPath, "*.mp3", SearchOption.AllDirectories).Where(x =>
{
var f = TagLib.File.Create(x);
return (((TagLib.Id3v2.Tag)f.GetTag(TagTypes.Id3v2)).Comment != null && ((TagLib.Id3v2.Tag)f.GetTag(TagTypes.Id3v2)).Comment.Contains(upit));
}
);
foreach (string f in matchingFiles)
{
System.IO.File.Copy(f, Path.Combine(path, new FileInfo(f).Name));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n" + ex.StackTrace);
}