【问题标题】:WebClient using file (file in use error)WebClient 使用文件(文件使用错误)
【发布时间】:2017-04-26 19:44:42
【问题描述】:

我是 WinForms/C#/VB.NET 的新手,我正在尝试组合一个简单的应用程序来下载 MP3 文件并编辑其 ID3 标签。到目前为止,这是我想出的:

Uri link = new System.Uri("URL");
wc.DownloadFileAsync(link, @"C:/music.mp3");
handle.WaitOne();

var file = TagLib.File.Create(@"C:/music.mp3");
file.Tag.Title = "Title";
file.Save();

顶部使用预定义的 WebClient 下载文件,但是当我尝试在后半部分的第一行打开文件时,我遇到了这个错误 The process cannot access the file 'C:\music.mp3' because it is being used by another process. 我猜这是由于网络客户端。

关于如何解决这个问题的任何想法?谢谢。

【问题讨论】:

  • 这似乎是你的答案stackoverflow.com/a/28217960/920557 我猜想是在再次打开文件之前处理客户端。
  • 如果您正在写入大多数系统不会授予您在 C:/ 的根目录上创建文件的权限,除非您的应用程序是管理员。您可能想尝试将其保存在子文件夹中。
  • 是的,@EugeneKomisarenko 是对的。在您的特定情况下不需要异步调用。只需将第二行替换为 wc.DownloadFile(link, @"C:/music.mp3"); 并删除第三行即可。
  • 您希望保留 Async 方法,否则 UI 将在下载过程中冻结。

标签: c# .net winforms


【解决方案1】:

如果使用WebClient.DownloadFileAsync,您应该订阅DownloadFileCompleted 事件并从该事件执行剩余的处理。

又快又脏:

WebClient wc = new WebClient();
wc.DownloadfileCompleted += completedHandler;
Uri link = new System.Uri("URL");
wc.DownloadFileAsync(link, @"C:/music.mp3");
//handle.WaitOne();  // dunno what this is doing in here.

function completedHandler(Object sender, AsyncCompletedEventArgs e) {
    var file = TagLib.File.Create(@"C:/music.mp3");
    file.Tag.Title = "Title";
    file.Save();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    相关资源
    最近更新 更多