WebClient DownloadFile正在被另一个进程使用

时间:2014-11-04 18:49:02

标签: c# webclient downloadfile

所以我收到一个错误,我的文件正由另一个进程使用。我相信这是因为它在进入using语句之前还没有完全完成下载?

下面是我的代码,我尝试将我的下载转换为异步并使用downloadcompletedevent,但仍然会产生同样的问题。还建议使用dispose函数来确保它释放文件,但这似乎也没有帮助。任何帮助将不胜感激。

if (file.path.Contains(".zip"))
{
    WebClient downloader = new WebClient();
    Console.WriteLine("http://directory.com/api/download" + file.path);
    string downloadLink = "http://directory.com/api/download" + file.path;
    downloader.DownloadFile(new Uri(downloadLink), "temp.ldif");
    downloader.Dispose();
    using (LdifEntryReader ldif = new LdifEntryReader(new LdifReader("temp.ldif")))
    {
        for (Entry entry = ldif.ReadEntry(); entry != null; entry = ldif.ReadEntry())
            Console.WriteLine("Found: {0}", entry.DistinguishedName);
    }
}

更新 在一些建议下并尝试了以下内容:

using (WebClient downloader = new WebClient())
{
    Console.WriteLine("http://directory.com/api/download" + file.path);
    string downloadLink = "http://directory.com/api/download" + file.path;
    downloader.DownloadFile(new Uri(downloadLink), @"temp.ldif");
}
using (LdifEntryReader ldif = new LdifEntryReader(new LdifReader("temp.ldif")))
{
    for (Entry entry = ldif.ReadEntry(); entry != null; entry = ldif.ReadEntry())
        Console.WriteLine("Found: {0}", entry.DistinguishedName);
}

0 个答案:

没有答案