private: WebClient ^WebClient1;
//*******************************
//*******************************
//********** FORM LOAD **********
//*******************************
//*******************************
private: System::Void frmInstallControlCentre_Load(System::Object^ sender, System::EventArgs^ e)
{
try
{
//----- CREATE DIRECTORY -----
if (!Directory::Exists(TEMP_DOWNLOAD_DIRECTORY))
Directory::CreateDirectory(TEMP_DOWNLOAD_DIRECTORY);
//----- DELETE FILE IF IT ALREADY EXISTS -----
try
{
if (File::Exists(TEMP_DOWNLOAD_DIRECTORY + "SomeFile.txt"))
{
//If file has read only attribute set clear it so that delete can occur
if ((File::GetAttributes(TEMP_DOWNLOAD_DIRECTORY + "SomeFile.txt") & FileAttributes::ReadOnly) == FileAttributes::ReadOnly)
File::SetAttributes(TEMP_DOWNLOAD_DIRECTORY + "SomeFile.txt", FileAttributes::Normal);
File::Delete(TEMP_DOWNLOAD_DIRECTORY + "SomeFile.txt");
}
}
catch (IOException ^)
{
GlobalObjects::EventLog1->AddEvent(true, 0x00, "Can't delete existing SomeFile.txt file");
}
//----- START THE DOWNLOAD -----
lblState->Text = "Downloading file...";
String ^url = "http://www.somedomain.com/SomeFile.txt";
// Create an instance of WebClient
WebClient1 = gcnew WebClient();
WebClient1->DownloadFileCompleted += gcnew AsyncCompletedEventHandler(this, &frmInstallControlCentre::DownloadFileCompleted);
WebClient1->DownloadProgressChanged += gcnew DownloadProgressChangedEventHandler(this, &frmInstallControlCentre::DownloadFileProgressCallback);
WebClient1->DownloadFileAsync(gcnew Uri(url), TEMP_DOWNLOAD_DIRECTORY + "SomeFile.txt");
}
catch (Exception ^e)
{
GlobalObjects::EventLog1->AddEvent(true, 0x00, Convert::ToString(e));
}
}
//************************************************
//************************************************
//********** DOWNLOAD PROGRESS CALLBACK **********
//************************************************
//************************************************
void DownloadFileProgressCallback(System::Object^ sender, System::Net::DownloadProgressChangedEventArgs ^ e)
{
if ((e->ProgressPercentage >= 0) && (e->ProgressPercentage <= 100))
DownloadProgressBar->Value = e->ProgressPercentage;
}
//************************************************
//************************************************
//********** DOWNLOAD COMPLETED CALLBACK **********
//************************************************
//************************************************
void DownloadFileCompleted(System::Object^ sender, System::ComponentModel::AsyncCompletedEventArgs^ e)
{
if (e->Cancelled)
{
//CANCELLED
}
else if (e->Error != nullptr)
{
//AN ERROR OCCURED
}
else
{
//SUCCESS
}
}
//***********************************
//***********************************
//********** CANCEL BUTTON **********
//***********************************
//***********************************
private: System::Void btnCancel_Click(System::Object^ sender, System::EventArgs^ e)
{
//Cancel the download if it is active
try
{
if (WebClient1 != nullptr)
WebClient1->CancelAsync();
}
catch (Exception ^)
{
}
this->Close();
}
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.