Post: Basic Updater For Your Program C#
07-13-2014, 04:58 AM #1
jagex
Gym leader
(adsbygoogle = window.adsbygoogle || []).push({}); Requirements:
(I will assume you are using dropbox)

1. Create 2 text files
2. Name on VersionNumber and inside it put your program version ex(1.0.0.1)
3. The second text file name is Downloadlink and insert the download link for your updated program

    
using System.Windows.Forms;
using System.Net;
using System.IO.Compression;


    
WebClient myWebclient = new WebClient();
string Temp;
string checkversion;


Add this code to where you want to start the update from
    
myWebclient.DownloadFileCompleted += OnDownloadComplete;
myWebclient.DownloadFileAsync(new Uri("URL for Version Number txt file goes here"), Temp + "VersionNumber.txt");




    
private void OnDownloadComplete(object sender, AsyncCompletedEventArgs e)
{
myWebclient.Dispose();
var reader = new System.IO.StreamReader(Temp + "VersionNumber.txt");
checkversion = reader.ReadToEnd();
reader.Dispose();
DownloadNewVersion();
}


    
private void DownloadNewVersion()
{
if (checkversion.Contains(Application.ProductVersion) )
{
MessageBox.Show("You have the latest version!", "Updater", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

}
else
{


MessageBox.Show("Update Found!" + "\n" + "Current Version:" + " " + Application.ProductVersion + "\n" +
"New Version:" + " " + checkversion, "Updater", MessageBoxButtons.OK, MessageBoxIcon.Information);
myWebclient.DownloadFile("URL for Downloadlink txt file goes here", Temp + "Downloadlink.txt");
var Downloadlink = new System.IO.StreamReader(Temp + "Downloadlink.txt");
string Link = Downloadlink.ReadToEnd();
Downloadlink.Dispose();
FolderBrowserDialog SelectFolder = new FolderBrowserDialog();
SelectFolder.ShowDialog();
string Location = SelectFolder.SelectedPath;
if (Location == "")
{
MessageBox.Show("No Directory Selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;

}
else
{

try {
MessageBox.Show("Update Found!" + "\n" + "Current Version:" + " " + Application.ProductVersion + "\n" +
"New Version:" + " " + checkversion, "Updater", MessageBoxButtons.OK, MessageBoxIcon.Information);
myWebclient.DownloadFile("https://dl.dropbox.com/s/30pbkeg3nfr662y/NewVersion.txt?dl=1", Temp + "Downloadlink.txt");
var Downloadlink = new System.IO.StreamReader(Temp + "Downloadlink.txt");
string Link = Downloadlink.ReadToEnd();
Downloadlink.Dispose();
FolderBrowserDialog SelectFolder = new FolderBrowserDialog();
SelectFolder.ShowDialog();
string Location = SelectFolder.SelectedPath;
if (Location == "")
{
MessageBox.Show("No Directory Selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);


}
else
{

System.IO.Directory.CreateDirectory(Location + @"\NewVersion");
myWebclient.DownloadFile(Link, Location + @"\NewVersion\Update.zip");
myWebclient.Dispose();
string path = Location;
Location = "";
string zipPath = path + @"\NewVersion\Update.zip";
string extractPath = path + @"\NewVersion";
ZipFile.ExtractToDirectory(zipPath, extractPath);
System.IO.File.Delete(path + @"\NewVersion\Update.zip");
MessageBox.Show("Download Complete!" + "\n" + "Download Destination:" + " " + path, "Updater", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
System.Diagnostics.Process.Start(path);
/*Application.Exit()*/
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}
}




Last edited by jagex ; 08-26-2014 at 05:26 PM.

The following 5 users say thank you to jagex for this useful post:

04jberry, Cyb3r, QuantumDev, ResistTheSteam, xKrazy SicknesS
08-23-2014, 09:44 PM #11
jagex
Gym leader
Tomorrow im going to work on the updater and improve it a lot!
Last edited by jagex ; 08-24-2014 at 05:29 AM.
08-25-2014, 02:26 AM #12
jagex
Gym leader
New one is almost done, much cleaner and easier to understand + it allows the user to save the file
08-26-2014, 02:25 AM #13
jagex
Gym leader
Ok I added the new code, easier to read and much cleaner!

I also added a new feature where the user gets to choose where to save the downloaded file to

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo