Wpf Listview异常进程由其他进程使用

时间:2017-03-14 19:26:29

标签: c# wpf listview

我从Listview中的文件夹加载Mp3文件。右键单击选项pn listview有一个更新标签的选项。当我输入字段数据并单击更新标签时,它会抛出ecception进程无法访问文件,这是其他进程使用的,这里是我的代码

private void UpdateTagEditor_RegisterActionEventHandler(object sender, RoutedEventArgs e)
{
    var tags = sender as UpdateTags;
    string path = tags.targetPath;
    string comments = tags.txtTagComment.Text;
    string lyrics = tags.txtTagLyrics.Text;
    try
    {
        using (var stream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.Read))
        {
            TagLib.Id3v2.Tag.DefaultVersion = 3;
            TagLib.Id3v2.Tag.ForceDefaultVersion = true;
            TagLib.File tagFile = TagLib.File.Create(path);
            tagFile.Tag.Comment = comments;
            tagFile.Tag.Lyrics = lyrics;
            tagFile.Save();
        }
    }
    catch (Exception exception)
    {

    }
}

1 个答案:

答案 0 :(得分:0)

你在使用Stream做什么?似乎没什么。您正在尝试两次创建该文件。尝试删除流:

private void UpdateTagEditor_RegisterActionEventHandler(object sender, RoutedEventArgs e)
{
    var tags = sender as UpdateTags;
    string path = tags.targetPath;
    string comments = tags.txtTagComment.Text;
    string lyrics = tags.txtTagLyrics.Text;
    try
    {
        TagLib.Id3v2.Tag.DefaultVersion = 3;
        TagLib.Id3v2.Tag.ForceDefaultVersion = true;
        TagLib.File tagFile = TagLib.File.Create(path);
        tagFile.Tag.Comment = comments;
        tagFile.Tag.Lyrics = lyrics;
        tagFile.Save();
    }
    catch (Exception exception)
    {

    }
}