using System;
using System.IO;
using System.Net;
using System.Diagnostics;
Next we are going to create a public static class so that we can reference it from our form
public static class Application_Updater
{
}
Now we must declare the CurrentVersion property so that we can match it to the latest version, add this code inside the class
private static string currentVersion;
public static string CurrentVersion { get; set; }
now for our string variables, add these below the "CurrentVersion" property
private static string ApplicationDomain = "Direct Download Link to Latest Application";
private static string LatestVersionTextDomain = "Direct Link to Latest Version Text File";
private static string CurrentLocation = AppDomain.CurrentDomain.BaseDirectory;
private static string CurrentName = AppDomain.CurrentDomain.FriendlyName;
private static string TemporaryName = Guid.NewGuid().ToString("N");
private static string CommandName = "\" + Guid.NewGuid().ToString("N") + ".cmd";
Now that everything has been declared we can move on to our methods and functions, first a method which will be a boolean and will check whether there is an update available or not
public static Boolean IsUpdateAvailable()
{
string LatestVersion;
try
{
using (WebClient Client = new WebClient())
{
LatestVersion = Client.DownloadString(LatestVersionTextDomain);
}
if (CurrentVersion != LatestVersion)
{
return true;
}
else
{
return false;
}
}
catch (WebException WebEx)
{
//You can do whatever you want with the caught exception.
System.Windows.Forms.MessageBox.Show(WebEx.Message);
return false;
}
catch (Exception ex)
{
//You can do whatever you want with the caught exception.
System.Windows.Forms.MessageBox.Show(ex.Message);
return false;
}
}
Second a function that will download our latest application and then call our second function to melt and replace
public static void UpdateProgram()
{
using (WebClient Client = new WebClient())
{
Client.DownloadFile(ApplicationDomain, CurrentLocation + "\" + TemporaryName);
}
MeltAndReplaceFile();
}
Finally our second function that will shutdown and delete our old application and then replace it with our new one and run it.
private static void MeltAndReplaceFile()
{
using (StreamWriter CMDWrite = new StreamWriter(CurrentLocation + CommandName, true))
{
CMDWrite.WriteLine("cd " + '\u0022' + CurrentLocation + '\u0022';
CMDWrite.WriteLine("Taskkill /F /IM " + '\u0022' + CurrentName + '\u0022';
CMDWrite.WriteLine("PING 1.1.1.1 -n 1 -w 1000 >NUL");
CMDWrite.WriteLine("TYPE nul > " + '\u0022' + CurrentName + '\u0022';
CMDWrite.WriteLine("DEL /F /S /Q /A " + '\u0022' + CurrentName + '\u0022';
CMDWrite.WriteLine("rename " + '\u0022' + TemporaryName + '\u0022' + " " + '\u0022' + CurrentName + '\u0022';
CMDWrite.WriteLine("start " + '\u0022' + '\u0022' + " " + '\u0022' + CurrentName + '\u0022';
CMDWrite.WriteLine("DEL /F /S /Q /A %0");
}
ProcessStartInfo StartCMD = new ProcessStartInfo(CurrentLocation + CommandName);
StartCMD.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(StartCMD);
}
if (Application_Updater.IsUpdateAvailable() == true)
{
MessageBox.Show("An update is available");
}
else
{
MessageBox.Show("You are using the latest version");
}
Now double click the second button and add this code
Application_Updater.UpdateProgram();
Finally we are going to set our current application version so in the form method inside your form code which should look like this
Application_Updater.CurrentVersion = "Your Application Version";
Step 4 - Compiling and DebuggingCopyright © 2024, NextGenUpdate.
All Rights Reserved.