2013-12-11 129 views
2

我目前的工作在客户端上,需要一个按钮,将检查目录,看看是否中存在一个特定的文件夹,如果没有它就会从下载一个.zip文件我web服务器,并且一旦完成下载,将所述.zip文件解压缩到目录。文件下载(不下载?)

出于某种原因,应用程序返回,它是完整的,但它完全不下载任何东西。我用了一个教程我在网上找到了一些修改,要做到这一点(http://www.ultimateprogrammingtutorials.info/2013/06/how-to-make-downloader-in-c.html)。

这里是我的下载代码:

下载链接:

private void btnDownload_Click(object sender, EventArgs e) 
    { 
     if (!Directory.Exists(HardCorpsPath)) 
     { 
      //MessageBox.Show("Downloading HardCorps Mod Pack (Full)", "Downloading", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      zipFileName = "HardCorps"; 

      HCDownloadURL = String.Format("http://www.survivaloperations.net/client/hardcorps/{0}.zip", zipFileName); 

      WebClient Download_Client = new WebClient();//Declaring the webclient as Download_Client 
      Download_Client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);//the event handler 
      Download_Client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);// " " 
      Download_Client.DownloadFileAsync(new Uri(HCDownloadURL.Trim().ToString()), Arma2OAPath);// " " 

      //extract zip 
      HCZipPath = Path.Combine(Arma2OAPath, @"HardCorps.zip"); 

      using (var zipFile = ZipFile.Read(HCZipPath)) 
      { 
       zipFile.ExtractAll(Arma2OAPath, ExtractExistingFileAction.OverwriteSilently); 
      } 


     } 
     else 
     { 
      MessageBox.Show("Directory Validated!"); 
      //Read users HardCorpsPath\version.txt and compare to server version.txt 
      //if server version > user version, download patch.zip where "patch" == the number version of the server's version.txt (ex: 1001.zip) 
      //On download complete, extract to HardCorpsPath and overwrite silently 
     } 
    }//close Download Button 

其余:

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
    { 
     pbDownloader.Value = e.ProgressPercentage;//setting the progressbar value as downloadprogress 
    } 
    private void Completed(object sender, AsyncCompletedEventArgs e) 
    { 
     MessageBox.Show("Downloading Successful ", "Download_Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);//just a messagebox   
     pbDownloader.Value = (0);//resetting the progressbar 
    } 

我越来越没有错误,直到应用程序尝试解压缩文件那不存在。

漂亮的失落和迷惘,可以使用一组全新的眼光来发现问题。

谢谢!

回答

2

你的问题很可能是你传递一个文件路径中DownloadFileAsync时,你应该传递一个文件名。

string fullFileName = Arma2OAPath + "test.zip"; 
Download_Client.DownloadFileAsync(new Uri(HCDownloadURL.Trim().ToString()), fullFileName); 

另一个问题是, DownloadFileAsync是非阻塞。这意味着您立即开始解压缩文件而无需等待下载。您应该将解压缩移动到Completed()

+0

我提出解压到完成()方法,并把它的消息以上(所以它告诉用户在下载完成之前提取物),但是现在我得到一个异常“无法读取,作为一个zip文件”使用DotNetZip 1.9,但它仍然提取正确的zip文件(只是抛出一个异常,因为它)。这里是例外http://pastebin.com/NTrtMgR9 – Meta