2014-01-20 1 views
0

이 코드를 가지고 있지만 그것은 작동하지 않습니다는 ...FTP 다운로드 BackgroundWorker에

ProgressBar를 움직되지 않고 다운로드 한 파일의 크기는 0킬로바이트입니다.

내 WHILE 루프에 문제가 있다고 생각합니다. 이 문제를 어떻게 해결할 수 있습니까? 나에게 지침을주세요!

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
{ 
    DirectoryInfo folder= new DirectoryInfo(@"C:\Cloud24"); 
    try 
    { 
    { 
     long size= 0; 
     WebClient request = new WebClient(); 
     request.Credentials = new NetworkCredential(userid, userpass); 
     FileStream file = File.Create(folder+ "//" + downloadname); 
     byte[] filedata = request.DownloadData(ftpadress + "/" + downloadname); 
     while ((size= file.Read(filedata, 0, filedata.Length)) > 0) 
     { 
     file.Write(filedata, 0, filedata.Length); 
     size += (int)filedata.Length; 
     double dProgressPercentage = ((double)(size)/(double)filedata.Length); 
     backgroundWorker1.ReportProgress((int)(dProgressPercentage * 100)); 
     } 
     file.Close(); 
     MessageBox.Show(downloadname + " downloaded!" + 
         Environment.NewLine + "There: " + folder); 
    } 
    } 
    catch (Exception exc) 
    { 
    MessageBox.Show("Error: " + exc.Message); 
    } 
} 
+0

디버깅을 시도 했습니까? – CloudyMarble

+0

예! 벌써 –

+0

을 debuged하면서 그 동안 루프 안에 들어가는거야? – CloudyMarble

답변

0

아마도 작동해야하지만 실제로 테스트하지는 않았습니다.

private async void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
    { 
     var folder = new DirectoryInfo(@"C:\Cloud24"); 
     try 
     { 
      { 
       var manualResetEvent = new ManualResetEventSlim(); 
       var client = new WebClient { Credentials = new NetworkCredential(userid, userpass) }; 
       client.DownloadProgressChanged += (o, args) => backgroundWorker1.ReportProgress(args.ProgressPercentage); 
       client.DownloadDataCompleted += (o, args) => manualResetEvent.Set(); 
       var filedata = client.DownloadDataAsync(ftpadress + "/" + downloadname); 
       manualResetEvent.Wait(); 
       using (var stream = File.Create(folder + "//" + downloadname)) 
       { 
        await stream.WriteAsync(filedata, 0, filedata.Length); 
       } 
       MessageBox.Show(downloadname + " downloaded!" + Environment.NewLine + "There: " + folder); 
      } 
     } 
     catch (Exception exc) 
     { 
      MessageBox.Show("Error: " + exc.Message); 
     } 
    } 
+0

var filedata = client.DownloadDataAsync (ftpadress + "/"+ downloadname); 잘못된 URI에 대한 오류 및 오류. –

+0

음 ... ftpadress + "/"+ downloadname은 (으)로 무엇을 평가합니까? 유효한 ftp 주소로 평가되는지 확인해야합니다. – EMB

+0

예 : ftp://contoso.com/testfile.txt. ftpadress는 "ftp://contoso.com"이고 다운로드 이름은 "testfile.txt"입니다 –