Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 复制文件时出现WebClient异常_C#_.net_Visual Studio 2010_Webclient - Fatal编程技术网

C# 复制文件时出现WebClient异常

C# 复制文件时出现WebClient异常,c#,.net,visual-studio-2010,webclient,C#,.net,Visual Studio 2010,Webclient,我想“下载”一个文件(更像是将它从一个目的地复制到另一个目的地),试图找到实现这一点的最佳方法。我已经试过xcopy等了。现在我在试WebClient。我的代码如下: WebClient client = new WebClient(); client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);

我想“下载”一个文件(更像是将它从一个目的地复制到另一个目的地),试图找到实现这一点的最佳方法。我已经试过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);
        }
WebClient=newWebClient();
client.DownloadProgressChanged+=新的DownloadProgressChangedEventHandler(client\u DownloadProgressChanged);
client.DownloadFileCompleted+=新的AsyncCompletedEventHandler(client\u DownloadFileCompleted);
foreach(位于_destRepository.Destinations中的字符串驱动路径)
{
做
{
AsyncItem作业=_repository.GetNextAsyncItem();
字符串源=job.DownloadLocation;
字符串目的地=驱动路径+作业。目的地;
DownloadFileAsync(新Uri(源),目标);
}而(_repository.QueueCount<1);
}
AsyncItem只是一个保存源和相对目标(目标没有驱动器位置)的自定义类。然后,它将给出其驱动器路径,然后客户端显示DownloadFileAsync。但是,在事件完成函数中,我得到一个错误。InnerException告诉我目标不存在

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

StackOverflow的观点是什么


谢谢

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

FileInfo dest = new FileInfo(destination);
if (!dest.Directory.Exists)
    dest.Directory.Create();
但第二,也是最重要的一点,WebClient不支持并发I/O操作