Post: [C#] Program / Application Updater Compatible with .Net 2.0 +
10-14-2015, 04:54 AM #1
Sloth
Banned
(adsbygoogle = window.adsbygoogle || []).push({});
Whats good fgtz so I know winter posted one of these already but I thought I'd share my method with you guys as well as it includes a file melting function Happy

Link to Winter Fgtz Thread
You must login or register to view this content.

In this tutorial I will be demonstrating how to create and implement code in to your C# project that will allow you to send out updates to end user application.


NOTE: The source to this project will be linked at the bottom along with a virus scan.



What You Need



    [*=left]A C# Development IDE (I will be using Visual Studio 2015 Professional)
    [*=left]A Direct Download Filehost for your latest application and your latest version text file (I will be using my own server, you can use dropbox or something similar if you wish)




Step 1 - Creating you project and setting up the form


Now you can either use and already created project or you can create a new one to work on, for this tutorial I will be creating a new Windows Form project to work on.


Now that we have our project open let's add some components to our form.


Add 2 Buttons to the form



Change the first buttons text to:
Originally posted by another user
Check For Update




Change the second buttons text to:
Originally posted by another user
Update Application



Step 2 - Writing the update class file


Now that the form is setup we are going to want to write our update class.


Create a new class and call it whatever you'd like, for this tutorial I will be calling the class "Application Updater.cs".
Now delete everything inside the file and we'll start from scratch.


First we will add our references
    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


In the "ApplicationDomain" string link directly to your latest exectuable version download
In the "LatestVersionTextDomain" string link directly to your hosted text file containing your latest application version (It can be formatted however you like e.g. "1" or "1.2.0.3")
    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'Winky Winky;
CMDWrite.WriteLine("Taskkill /F /IM " + '\u0022' + CurrentName + '\u0022'Winky Winky;
CMDWrite.WriteLine("PING 1.1.1.1 -n 1 -w 1000 >NUL");
CMDWrite.WriteLine("TYPE nul > " + '\u0022' + CurrentName + '\u0022'Winky Winky;
CMDWrite.WriteLine("DEL /F /S /Q /A " + '\u0022' + CurrentName + '\u0022'Winky Winky;
CMDWrite.WriteLine("rename " + '\u0022' + TemporaryName + '\u0022' + " " + '\u0022' + CurrentName + '\u0022'Winky Winky;
CMDWrite.WriteLine("start " + '\u0022' + '\u0022' + " " + '\u0022' + CurrentName + '\u0022'Winky Winky;
CMDWrite.WriteLine("DEL /F /S /Q /A %0");
}
ProcessStartInfo StartCMD = new ProcessStartInfo(CurrentLocation + CommandName);
StartCMD.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(StartCMD);
}



Step 3 - Calling our Functions and Methods from our form


Now in our form we are going to want to call our methods and functions so we can check for an update and then update the application if there is one

Remember that it's called via "namespace.Function or namespace.method"


double click the first button and in the click event add this code
    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


You must login or register to view this content.


add this code (the version can be in any format but make sure it exactly matches the latest version text document e.g. "1" or "1.2.0.3")
    Application_Updater.CurrentVersion = "Your Application Version";
Step 4 - Compiling and Debugging


Now all that is left is for you to compile and debug the application and test if it works.


if you have any issues with getting this to work or find an issue with my code please let me know and I'll do my best to help you out.


Downloads


The 8 Detections are false positives, if you don't trust it just copy the code from the tutorial.
Virus Scan: You must login or register to view this content.
Direct Download Link: You must login or register to view this content.

Last edited by Sloth ; 10-14-2015 at 05:01 AM.
10-14-2015, 05:44 AM #2
Winter
Purple God

The following user thanked Winter for this useful post:

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo