c# - OFD 的文件路径不适用于 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();
}
}
解决方案
尝试使用
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
}
推荐阅读
- react-native - 自定义字体图标在 react-native 的框中显示交叉
- generics - 在 F# 中强制内部 DSL 的现有类型
- nativescript - NativeScript 使用 2way 数据绑定
- python - python urllib:构建 url,包括带和不带关键字的参数
- javascript - 修复 eslint 错误 - 调用 require() 应使用字符串文字
- xamarin - 有什么方法可以找到使用 Xamarin 从 XAML 创建的 C# 代码?
- python - 用 Python 编写 CSV 的问题
- firebase - GitHub 持续部署到 Firebase 托管和环境变量
- c# - 在没有绑定的代码隐藏中预选 CollectionView
- api - 在 UI 中列出 env/config 变量是否不安全?