【问题标题】:WebClient Exception when copying a file复制文件时出现 WebClient 异常
【发布时间】:2012-11-14 09:39:08
【问题描述】:

我想“下载”一个文件(更像是从一个目的地复制到另一个目的地),以尝试找到执行此操作的最佳方法。我已经尝试过 xcopy 等。现在我正在尝试 WebClient。我的代码如下:

        WebClient client = new WebClient();
        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
        client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);

        foreach (string drivePath in _destRepository.Destinations)
        {
            do
            {
                AsyncItem job = _repository.GetNextAsyncItem();
                string source = job.DownloadLocation;
                string destination = drivePath + job.Destination;
                client.DownloadFileAsync(new Uri(source), destination);
            } while (_repository.QueueCount < 1);
        }

AsyncItem 只是一个包含源和相对目标(没有驱动器位置的目标)的自定义类。然后将给出其驱动器路径,然后客户端显示 DownloadFileAsync。但是在 Event Completed 函数中我得到一个错误。 InnerException 告诉我目的地不存在?

当然它还不存在,WebClient 必须做到。这让我相信也许 WebClient 不会创建文件夹结构?其中一些文件位于两层深处。

StackOverflow 的意见是什么?

谢谢。

【问题讨论】:

    标签: c# .net visual-studio-2010 webclient


    【解决方案1】:

    上面的简单答案是肯定的,WebClient 不会为您创建文件夹结构。必须通过执行以下操作来添加它们:

    FileInfo dest = new FileInfo(destination);
    if (!dest.Directory.Exists)
        dest.Directory.Create();
    

    但其次也是最重要的 WebClient 不支持 CONCURRENT I/O 操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-08
      相关资源
      最近更新 更多