Reputation: 1860
I am currently working on a client that requires a button that will check a directory to see if a specific folder exists within it, if not it will then download a .zip file from my web server and once completed downloading, extract said .zip file to a directory.
For some reason the application returns that it is complete, however it does not download anything at all. I used a tutorial I found online to do this (http://www.ultimateprogrammingtutorials.info/2013/06/how-to-make-downloader-in-c.html) with some modifications.
Here is my download code:
The Download Button:
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
The Rest:
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
}
I am getting no errors until the application attempts to unzip a file that does not exist.
Pretty lost and confused, could use a set of fresh eyes to spot the problem.
Thanks!
Upvotes: 2
Views: 757
Reputation: 9426
Your issue is likely that you're passing a file path in to DownloadFileAsync
when you should be passing in a file name.
string fullFileName = Arma2OAPath + "test.zip";
Download_Client.DownloadFileAsync(new Uri(HCDownloadURL.Trim().ToString()), fullFileName );
Another issue is that
DownloadFileAsync is non-blocking. Which means that you immediately start unzipping the file without waiting for it to download. You should move the unzipping to Completed()
.
Upvotes: 2