Created
April 28, 2018 19:48
-
-
Save relliv/581b07fa929f7c91b20410f4eedabc4c to your computer and use it in GitHub Desktop.
C# Basic App Updater
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel; | |
using System.IO; | |
using System.Net; | |
using System.Windows; | |
namespace Basic_Updater_Example | |
{ | |
/// <summary> | |
/// Interaction logic for MainWindow.xaml | |
/// </summary> | |
public partial class MainWindow : Window | |
{ | |
public string version = "2.1"; | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
ClientVersion.Text = "Your App Version: " + version; | |
} | |
/// <summary> | |
/// Update Check Button | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
private void Button_Click(object sender, RoutedEventArgs e) | |
{ | |
// cerate ne web client for web request | |
WebClient webclient = new WebClient(); | |
// open and read web page | |
Stream stream = webclient.OpenRead("http://localhost/updatecheck.php?version=" + version); | |
// stream content | |
StreamReader reader = new StreamReader(stream); | |
// get result | |
string result = reader.ReadToEnd(); | |
if (result == "OK") | |
{ | |
// ask to user would you like download this update/s | |
if (MessageBox.Show("Do you want to update application?", "There is an update", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) | |
{ | |
DownloadUpdate(); | |
} | |
} else if (result == "NO") | |
{ | |
// say no update | |
MessageBox.Show("Your app is Up to date", "There is not an update", MessageBoxButton.OK, MessageBoxImage.Information); | |
} else if (result == "RETURN") | |
{ | |
// ask to user would you like download this update/s | |
if (MessageBox.Show("Do you want to update application?", "There is an update", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) | |
{ | |
DownloadUpdate(); | |
} | |
} | |
} | |
/// <summary> | |
/// Update Downloader Method | |
/// </summary> | |
public void DownloadUpdate() | |
{ | |
// cerate ne web client for file download | |
WebClient webclient = new WebClient(); | |
// we need progress changed event for progress | |
webclient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgress); | |
// when calls download complated | |
webclient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadCompleted); | |
// set file download url, save folder and start downlaoding | |
string filedownlaodurl = "http://localhost/file.exe"; | |
// Anlık olarak indirilecek olan dosya ".exe"nin olduğu konuma kaydedilir | |
webclient.DownloadFileAsync(new Uri(filedownlaodurl), Path.GetFileName(filedownlaodurl)); | |
} | |
/// <summary> | |
/// WebClient Download Progress Changed Event | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
/// <see cref="https://msdn.microsoft.com/en-us/library/system.net.downloadprogresschangedeventargs"/> | |
private void DownloadProgress(object sender, DownloadProgressChangedEventArgs e) | |
{ | |
// download progress | |
DownlaoadProgress.Value = e.ProgressPercentage; | |
// downloaded bytes | |
Downloaded.Text = e.BytesReceived.ToString(); | |
// total bytes | |
Total.Text = e.TotalBytesToReceive.ToString(); | |
// download percentage as string | |
DownloadPercentage.Text = e.ProgressPercentage.ToString("{0}%"); | |
} | |
/// <summary> | |
/// WebClient AsyncCompleted Event Handler | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
/// <see cref="https://msdn.microsoft.com/tr-tr/library/system.componentmodel.asynccompletedeventhandler"/> | |
private void DownloadCompleted(object sender, AsyncCompletedEventArgs e) | |
{ | |
MessageBox.Show("Download completed", "Successful", MessageBoxButton.OK, MessageBoxImage.Information); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment