Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, February 3, 2010 5:02 AM
I want to make a check IF the file download is completed then do something...
for example in this code i want to make that when the file download is complete turn on the download button to TRUE again .
I did that after you press the download button is false the button . Now i want that if the download is completed make the download button true again .
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.Net;
using
System.IO;
namespace
WindowsFormsApplication1
{
public partial class Form1 : Form
{
WebClient Client = new WebClient();
ProgressBar prgBar = new ProgressBar();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Stream strm = Client.OpenRead("http://www.ims.gov.il/Ims/Pages/RadarImage.aspx?Row=9&TotalImages=10&LangID=1&Location=");
StreamReader sr = new StreamReader(strm);
Client.DownloadFile(
"http://www.ims.gov.il/Ims/Pages/RadarImage.aspx?Row=9&TotalImages=10&LangID=1&Location=", @"d:\testfile\untitled.bmp");
progressBar1.Maximum = 10;
progressBar1.Minimum = 0;
progressBar1.TabIndex = 0;
progressBar1.Value = 0;
progressBar1.Step = 10;
progressBar1.PerformStep();
button1.Enabled =
false;
sr.Close();
strm.Close();
strm.Flush();
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
FileInfo MyFile = new FileInfo(@"d:\testfile\untitled.bmp");
MyFile.Delete();
}
}
}
I want to check using IF () the file download is completed and only when the file is completed do something in the if function .
And if there is a way to turn the download button to true again after download is completed without using if () please show me too .
Show me both option with If() and without .
Thanks .
danieli
All replies (6)
Wednesday, February 3, 2010 5:43 AM ✅Answered
This is a blocking call so after this line is complete the file has been downloaded. If you want to validate that the call was successful you can use System.io.file.exists method.A programmer Trapped in a thugs body
Wednesday, February 3, 2010 9:54 AM ✅Answered | 1 vote
You shall apply the WebClient class and there
you have two different events. These events are
DownloadDataCompleted and
DownloadFileCompleted events.Coder24.com
Thursday, February 4, 2010 6:17 AM ✅Answered
hi,
jst follow this link
http://www.geekpedia.com/tutorial179_Creating-a-download-manager-in-Csharp.html
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
namespace DownloadManager
{
public partial class Form1 : Form
{
// The thread inside which the download happens
private Thread thrDownload;
// The stream of data retrieved from the web server
private Stream strResponse;
// The stream of data that we write to the harddrive
private Stream strLocal;
// The request to the web server for file information
private HttpWebRequest webRequest;
// The response from the web server containing information about the file
private HttpWebResponse webResponse;
// The progress of the download in percentage
private static int PercentProgress;
// The delegate which we will call from the thread to update the form
private delegate void UpdateProgessCallback(Int64 BytesRead, Int64 TotalBytes);
public Form1()
{
InitializeComponent();
}
private void btnDownload_Click(object sender, EventArgs e)
{
// Let the user know we are connecting to the server
lblProgress.Text = "Download Starting";
// Create a new thread that calls the Download() method
thrDownload = new Thread(Download);
// Start the thread, and thus call Download()
thrDownload.Start();
}
private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
{
// Calculate the download progress in percentages
PercentProgress = Convert.ToInt32((BytesRead * 100) / TotalBytes);
// Make progress on the progress bar
prgDownload.Value = PercentProgress;
// Display the current progress on the form
lblProgress.Text = "Downloaded " + BytesRead + " out of " + TotalBytes + " (" + PercentProgress + "%)";
}
private void Download()
{
using (WebClient wcDownload = new WebClient())
{
try
{
// Create a request to the file we are downloading
webRequest = (HttpWebRequest)WebRequest.Create(txtUrl.Text);
// Set default authentication for retrieving the file
webRequest.Credentials = CredentialCache.DefaultCredentials;
// Retrieve the response from the server
webResponse = (HttpWebResponse)webRequest.GetResponse();
// Ask the server for the file size and store it
Int64 fileSize = webResponse.ContentLength;
// Open the URL for download
strResponse = wcDownload.OpenRead(txtUrl.Text);
// Create a new file stream where we will be saving the data (local drive)
strLocal = new FileStream(txtPath.Text, FileMode.Create, FileAccess.Write, FileShare.None);
// It will store the current number of bytes we retrieved from the server
int bytesSize = 0;
// A buffer for storing and writing the data retrieved from the server
byte[] downBuffer = new byte[2048];
// Loop through the buffer until the buffer is empty
while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
// Write the data from the buffer to the local hard drive
strLocal.Write(downBuffer, 0, bytesSize);
// Invoke the method that updates the form's label and progress bar
this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, fileSize });
}
MessageBox.Show("Download Completed!");
}
finally
{
// When the above code has ended, close the streams
strResponse.Close();
strLocal.Close();
}
}
}
private void btnStop_Click(object sender, EventArgs e)
{
// Close the web response and the streams
webResponse.Close();
strResponse.Close();
strLocal.Close();
// Abort the thread that's downloading
thrDownload.Abort();
// Set the progress bar back to 0 and the label
prgDownload.Value = 0;
lblProgress.Text = "Download Stopped";
}
}
}
Nagarjuna Dilip
Wednesday, February 3, 2010 7:31 AM
hi,
http://www.geekpedia.com/tutorial179_Creating-a-download-manager-in-Csharp.htmlNagarjuna Dilip
Thursday, February 4, 2010 4:52 AM
hi,
did u try the above suggestions..........? feel free to post ur queries.......Nagarjuna Dilip
Thursday, March 24, 2011 2:55 AM
Hello,
Considering that many developers in this forum ask how to implement the progress for downloading, my team has created a code sample for this frequently asked programming task in Microsoft All-In-One Code Framework. You can download the code samples at:
With these code samples, we hope to reduce developers’ efforts in solving the frequently asked
programming tasks. If you have any feedback or suggestions for the code samples, please email us: [email protected].
The Microsoft All-In-One Code Framework (http://1code.codeplex.com) is a free, centralized code sample library driven by developers' needs. Our goal is to provide typical code samples for all Microsoft development technologies, and reduce developers' efforts in solving typical programming tasks.
Our team listens to developers’ pains in MSDN forums, social media and various developer communities. We write code samples based on developers’ frequently asked programming tasks, and allow developers to download them with a short code sample publishing cycle. Additionally, our team offers a free code sample request service. This service is a proactive way for our developer community to obtain code samples for certain programming tasks directly from Microsoft.
Thanks
Microsoft All-In-One Code Framework