public bool Download(String fileLocation)
{
try
{
// Setup the connection
var downloadRequest = (HttpWebRequest) WebRequest.Create(_url);
downloadRequest.Accept = "application/octet-stream";
downloadRequest.Method = WebRequestMethods.Http.Get;
downloadRequest.UserAgent = "SDownload";
// Start the connection to the github api
var downloadResponse = downloadRequest.GetResponse();
// Delete the local file if it already exists
if (File.Exists(fileLocation))
File.Delete(fileLocation);
// Open the file for writing
using (var installer = File.OpenWrite(fileLocation))
{
var installerBuffer = new byte[4096];
var responseStream = downloadResponse.GetResponseStream();
if (responseStream == null)
return false;
progressBar.Maximum = _size;
int read;
while ((read = responseStream.Read(installerBuffer, 0, installerBuffer.Length)) > 0)
{
// Write the info from the stream
installer.Write(installerBuffer, 0, read);
// Update the progress
progressBar.Step = read;
progressBar.PerformStep();
}
}
}
catch (Exception e)
{
// Store the last exception
LastException = e;
return false;
}
return true;
}