OFD的文件路径不适用于TagLib

时间:2018-07-28 09:37:02

标签: c# taglib

我想在单击按钮后通过打开文件对话框选择mp3文件,并将文件名更改为已指定的字符串。问题是,当我将文件路径TagLib.File.Create()插入为变量时,出现FileNotFound异常。 这是代码:

public partial class Form1 : Form
{
    OpenFileDialog ofd = new OpenFileDialog();

    string location = null;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ofd.ShowDialog();
        location = ofd.SafeFileName;
        var target = TagLib.File.Create(location);

        target.Tag.Title = "it works";
        target.Save();
    }
}

1 个答案:

答案 0 :(得分:1)

尝试使用

 location = ofd.FileName;

获取完整的文件路径,而不是

location = ofd.SafeFileName;

为您提供文件名。

最佳做法是:

TagLib.File target = null;
        if (!string.IsNullOrEmpty(location) && File.Exists(location))
        {
             target = TagLib.File.Create(location);
        }
        else
        {
            //log or print a warning 
        }