.NET Simple Auto Updating Feature for C# Applicatons

Chris7S

Nerd by definition, programmer by trade
Messages
1,522
Reaction score
490
The other day I ran across this thread asking about a auto updating feature for an application. Since I wrote some code for him I decided to make a post showing other users that may be need this as well to learn from it and hopefully implement it into your applications. You can download the project files from GitHub [Click here to view this link]!

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoUpdate
{
public partial class Form1 : Form
{
//Declare global variable(s)
string newVersion = "";

public Form1()
{
InitializeComponent();
}

//Create method to execute the update download
public void DownloadUpdate()
{
//URL of the updated file
string url = "https://www.dropbox.com/s/awu5t91jj6aken7/AutoUpdate.exe?dl=1";

//Declare new WebClient object
WebClient wc = new WebClient();
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
wc.DownloadFileAsync(new Uri(url), Application.StartupPath + "/AutoUpdate(1).exe");
}

void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
//Show a message when the download has completed
MessageBox.Show("Your application is now up-to-date!\n\nThe application will now restart!", "Update Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Restart();
}

//Create method to check for an update
public void GetUpdate()
{
//Declare new WebClient object
WebClient wc = new WebClient();
string textFile = wc.DownloadString("https://www.dropbox.com/s/mtdgffxqsalidil/AutoUpdaterVersion.txt?dl=1");
newVersion = textFile;
label1.Text = label1.Text + Application.ProductVersion;
label2.Text = label2.Text + newVersion;

//If there is a new version, call the DownloadUpdate method
if (newVersion != Application.ProductVersion)
{
MessageBox.Show("An update is available! Click OK to download and restart!", "Update Available", MessageBoxButtons.OK, MessageBoxIcon.Information);
DownloadUpdate();
}
}

private void Form1_Load(object sender, EventArgs e)
{
//Check for the update
GetUpdate();
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//This renames the original file so any shortcut works and names it accordingly after the update
if (System.IO.File.Exists(Application.StartupPath + "/AutoUpdate(1).exe"))
{
System.IO.File.Move(Application.StartupPath + "/AutoUpdate.exe", Application.StartupPath + "/AutoUpdate(2).exe");
System.IO.File.Move(Application.StartupPath + "/AutoUpdate(1).exe", Application.StartupPath + "/AutoUpdate.exe");
System.IO.File.Delete(Application.StartupPath + "/AutoUpdate(2).exe");
}
}
}
}
 
Last edited by a moderator:

FuzzyGaming45

Newbie
Messages
11
Reaction score
1
what about its checking just in case the user clicks the same button then it will re update

if you have the code please reply
 

Chris7S

Nerd by definition, programmer by trade
Messages
1,522
Reaction score
490
It doesn't even check if its up to date lmfao!
You're dumb...

Code:
//If there is a new version, call the DownloadUpdate method
if (newVersion != Application.ProductVersion)
{
MessageBox.Show("Ante is available! Click OK to download and restart!", "Update Available", MessageBoxButtons.OK, MessageBoxIcon.Information);
DownloadUpdate();
}
}
 
Messages
12
Reaction score
0
You're dumb...

Code:
//If there is a new version, call the DownloadUpdate method
if (newVersion != Application.ProductVersion)
{
MessageBox.Show("Ante is available! Click OK to download and restart!", "Update Available", MessageBoxButtons.OK, MessageBoxIcon.Information);
DownloadUpdate();
}
}
File is not replacing if download is completed 
File is not replacing after Download ! 
if (System.IO.File.Exists(Application.StartupPath + "/AutoUpdate(1).exe")) { System.IO.File.Move(Application.StartupPath + "/AutoUpdate.exe", Application.StartupPath + "/AutoUpdate(2).exe"); System.IO.File.Move(Application.StartupPath + "/AutoUpdate(1).exe", Application.StartupPath + "/AutoUpdate.exe"); System.IO.File.Delete(Application.StartupPath + "/AutoUpdate(2).exe"); } }

This code is not working !
 

Immense

Developer
Messages
262
Reaction score
200
File is not replacing if download is completed 
File is not replacing after Download ! 



This code is not working !
I did post everything you need for the auto updating on another thread. If you want I can link it here.

Edit: Matter of fact here I will just post it lol
Code:
public static string version = "version number here also make sure it matches the pastebin version number"; //version number
public static string weburl = "pastebin.com/raw/Ejyi62q0"; //Dont forget your https://
Place these under your partial class ^

Then you need your function webclient for downloading so here
public static void DownloadFile(string website, string name) //Clearly says what it does
        {
            WebClient myWebClient = new WebClient();
            myWebClient.DownloadFile(website, "" + Application.StartupPath + @"\Downloads\" + name);
        }

For the checking for update function here you go
public static void CheckForUpdate()
        {
            string[] lines = new WebClient().DownloadString(weburl).Split("\n".ToCharArray()); //using a string array so we can select the lines of text we want to use
            if (!lines[0].Contains(version)) //if it doesnt contain the current version on pastebin then update
            {
                WebClient myWebClient = new WebClient(); //allowing us to use basic internet functions
                myWebClient.DownloadFile(lines[1], "" + Application.StartupPath + @"\name of rar file here"); //downloads file
                Environment.Exit(0); //closes program
            }
        }

You also will need to place your Update Checking function under InitializeComponent();
So CheckForUpdate();

There you go very easy and simple for pastebin auto updating
Don't tell me this doesn't work for you. If you have any questions regarding the code then dm me or something, although the code basically says what it does. I edited my old post and labeled everything for you so you know what does what.
 
Last edited:
Messages
12
Reaction score
0
Bro am also a programmer !

In that code u had written Application.restart();

But if app is restarted how it delete ?

Application must close because app won't delete.
 

Immense

Developer
Messages
262
Reaction score
200
Bro am also a programmer !

In that code u had written Application.restart();

But if app is restarted how it delete ?

Application must close because app won't delete.
Ignore the OP's post and look at mine so you actually get what you are asking for since you seem to want everything handed to you. It's all there, and explained. Use it if you want.
 

Immense

Developer
Messages
262
Reaction score
200
I also presented another option to you which would be by sending me a dm.
 

Immense

Developer
Messages
262
Reaction score
200
Atleast gimme ur email bro so I can keep contact wid you
Clearly you don't understand what I am saying to you. I am saying to send me a direct message on here. If not then clearly you didn't want help enough, although everything you need is in my post above.
 

Immense

Developer
Messages
262
Reaction score
200
I want help !

My question is. After downloading my app from webclient. How to replace it with new app ?
Man just put everything in a rar file. You can simply have the rar archive download, from there they can extract what is needed from it.
 
Messages
12
Reaction score
0
I added .exe file in 000webhostapp.com
After I download that I wanna delete old one and replace new one brother. Thatsa alll

Let me show my code
 
Top Bottom