【问题标题】:File path from OFD not working with TagLibOFD 的文件路径不适用于 TagLib
【发布时间】:2018-07-28 09:37:01
【问题描述】:

我想在单击按钮后通过打开文件对话框选择一个 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();
    }
}

【问题讨论】:

    标签: c# taglib


    【解决方案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 
            }
    

    【讨论】:

    • 非常感谢老兄!
    猜你喜欢
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 2020-04-19
    • 1970-01-01
    • 2012-08-29
    • 2011-07-12
    相关资源
    最近更新 更多