2012-11-14 4 views
0

이 작업을 수행하는 가장 좋은 방법을 찾으려고 파일을 "다운로드"(한 대상에서 다른 대상으로 복사하는 것과 같이)하고 싶습니다. 지금까지 xcopy 등을 시도했습니다. 이제 WebClient를 시도하고 있습니다. 나는 아래의 코드는 배치했다 :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가 폴더 구조를 만들지 못할 수도 있습니다. 이 파일 중 일부는 2 단계 깊이 앉아 있습니다.

StackOverflow에 대한 의견은 무엇입니까?

감사합니다.

답변

0

위의 간단한 대답은 예입니다. WebClient가 폴더 구조를 만들지 않습니다. 그들은 다음을 수행하여 추가 할 수 있습니다

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

그러나 두 번째로 가장 중요한 웹 클라이언트 동시 I/O 작업을 지원하지 않습니다.