public Download ( String fileLocation ) : bool | ||
fileLocation | String | The local location to save the file to |
return | bool |
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;
}
/// <summary> /// Downloads and installs the newest version /// </summary> /// <param name="size">The size of the new version to download</param> private void DownloadAndInstall(int size) { var downloader = new DownloadProgressDialog(_fileUrl, size); var fileLocation = String.Format("{0}\\sdownload_update.exe", Path.GetTempPath()); try { LogUpdate(); } catch (Exception) { } if (downloader.Download(fileLocation)) { // Launch the installer and close the running instance try { Process.Start(fileLocation); } catch (Exception) { CrashHandler.Throw("There was an issue launching the update! You'll need to manually start the file: " + fileLocation, false); } Close(); Application.Exit(); } else { // There was an issue downloading the file CrashHandler.Throw("There was an issue downloading the update!", downloader.LastException); Close(); } }