C#:下载视频文件(MP4)会导致WebException

时间:2018-05-12 18:45:48

标签: c# .net

在各种下载过程中,我似乎得到了另一个进程已经使用过的文件。"在webClient.DownloadFile行下载文件时。

之前没有下载此文件,那么如何在其他地方打开?这似乎只发生在我下载MP4类型的文件时,这很奇怪。

例外:

  

System.Net.WebException:WebClient期间发生异常   请求。 ---> System.IO.IOException:进程无法访问   档案' M:\ blah \ blah \ blah'因为它正被另一个进程使用。

代码:

public static class DownloadUtilities
{
    public static int FailedDownloadCount;

    public static void DownloadLinks(List<AFile> files)
    {
        FailedDownloadCount = 0;

        Parallel.ForEach(
            files,
            new ParallelOptions { MaxDegreeOfParallelism = 20 },
            currentFile => { DownloadFile(currentFile); }
        );
    }

    private static void DownloadFile(AFile file)
    {
        try
        {
            using (var webClient = new WebClient())
            {
                webClient.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)");
                webClient.DownloadFile(new Uri(file.Address), file.SaveLocation);
            }
        }
        catch (WebException e)
        {
            Logger.Warn(e.ToString());
        }

        System.Threading.Thread.Sleep(250);
    }
}

0 个答案:

没有答案