I am creating a windows application using C#. It is used to check whether any updated version of a product is available or not. If yes, the user can download it using the application's UI only without opening any browser.
One window in the application displays the status of the download using ProgressBar control. The problem is, in case the internet gets disconnected, the application does not come to know. Say, after 45% of the download, the network disconnects; but the ProgressBar keeps on displaying 45%.
Is there any property/event that is raised once such situation occurs? Please help. Attaching my code as well for your reference. Thanks.
private void CheckForUpdate_Load(object sender, EventArgs e)
{
string downloadURL = Convert.ToString(ConfigurationManager.AppSettings["TempDownloadURL"]);
WebClient wcDownloadFile = new WebClient();
Uri myUri = new Uri(downloadURL);
wcDownloadFile.DownloadFileAsync(myUri, downloadLocation);
wcDownloadFile.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wcDownloadFile_DownloadProgressChanged);
}
void wcDownloadFile_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
try
{
int bytesDownloaded = Int32.Parse(e.BytesReceived.ToString());
int totalBytes = Int32.Parse(e.TotalBytesToReceive.ToString());
progBarSoftPhone.Value = e.ProgressPercentage;
lblStatus.Text = (bytesDownloaded / 1024).ToString() + " KB out of " + (totalBytes / 1024).ToString() + " KB downloaded (" + e.ProgressPercentage.ToString() + "%).";
}
catch (Exception ex)
{
MessageBox.Show("ERROR: " + ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}