.NET [FULL] Auto Updater

Discussion in 'Source Code & Tutorial Database' started by pLTPF, Dec 17, 2017 with 2 replies and 1,282 views.

  1. pLTPF

    pLTPF I am NOT LeafyIsHere

    Messages:
    2,160
    Ratings:
    524
    Here's a quick run down of what the the code's doing
    1. Checks the current version with the newest which is stored on a pastebin(can be any raw text viewer)
    2. If they don't match, it'll open the updater then close the current program(out of date program)
    3. The updater will delete the outdated version then download the new version.
    4. After it's downloaded it will close and open the updated version of the original tool.


    Console Application Code
    Code:
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;
    using System.Diagnostics;
    
    namespace Updater
    {
        class Program
        {
            // Credits to LTPF
            static void Main(string[] args)
            {
                WebClient wc = new WebClient();
                if (File.Exists("AutoUpdater.exe")) // Should be the pld out dated version.
                    File.Delete("AutoUpdater.exe");
                File.WriteAllBytes("AutoUpdater.exe", wc.DownloadData("http://1.3.3.7/AutoUpdater.exe")); // Creates and writes a file as it reads from the newest verison.
                Process.Start("AutoUpdater.exe"); // Launches the original program.
                Console.WriteLine("Update downloaded!\n");
            }
        }
    }
    
    For the 1.3.3.7 URL I used my server, I don't know of any file storage online where you can just type the URL in and it will immediately download.

    Form Code
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Net;
    using System.IO;
    using System.Diagnostics;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            string currentVersion = "1.0.0.0";
    
            private void Form1_Load(object sender, EventArgs e)
            {
                label1.Text = "Version: " + currentVersion;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                WebClient wc = new WebClient();
                string newVersion = wc.DownloadString("https:\\(raw text viewer containing only version, e.g. 1.0.0.0)"); // Downloads the newest version.
                if (currentVersion != newVersion)
                {
                    if (!File.Exists("Updater.exe")) // Checks if the updater file doesn't exist or is renamed.
                        MessageBox.Show("Updater not found! Please redownload the program!");
                    else
                    {
                        Process.Start("Updater.exe"); // Launches the updater if the file DOES exist.
                        Close();
                    }
                }
                else
                {
                    MessageBox.Show("Up to date!");
                }
            }
        }
    }
    
    For you code monkeys here's the sources to the applications I made
    [Click here to view the link]| Virus Scan | Photo Of Scan

    Insane amount of false positives on the virus total, I have no idea why there's no obfuscation on the source.​

     
    • Like Like x 2
  2. CeratedSuperior

    CeratedSuperior No sympathy for the Devil; keep that in mind

    Messages:
    725
    Ratings:
    198
    I'm sure people have found ways around it by now but I ran into issues using a different method by Immense (the same concept using pastebin to pull the URL) and most file hosters like mediafire won't work consistently because it will generate a new variant of the URL or either download the html page. With Dropbox there is something like "?dl=0" at the very end of the share link and I changed the 0 to 1 which made it download directly every time. Using mediafire I tried to right click the download button and use that link but it only works once. Just in case anyone else runs into the same issue.

    Good tutorial btw
     
  3. Chris7S

    Chris7S Nerd by definition, programmer by trade

    Messages:
    1,508
    Ratings:
    471
    pLTPF here is a better way to handle that with EventHandlers. This ensures your application does not step on its own toes, so to speak.

    [Click here to view the link]
     

Share This Page