.NET Simple Auto Updating Feature for C# Applicatons

Discussion in 'Source Code & Tutorial Database' started by Chris7S, Nov 6, 2016 with 31 replies and 10,569 views.

  1. Chris7S

    Chris7S Nerd by definition, programmer by trade

    Messages:
    1,508
    Ratings:
    479
    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 the 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");
    }
    }
    }
    }
    

     
    • Informative Informative x 3
    Last edited by a moderator: Nov 6, 2016
  2. BoliBerrys

    BoliBerrys Newbie

    Messages:
    1
    Ratings:
    0
  3. OP
    Chris7S

    Chris7S Nerd by definition, programmer by trade

    Messages:
    1,508
    Ratings:
    479
    I appreciate it man!
     
  4. FuzzyGaming45

    FuzzyGaming45 Newbie

    Messages:
    11
    Ratings:
    0
    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
     
  5. FuzzyGaming45

    FuzzyGaming45 Newbie

    Messages:
    11
    Ratings:
    0
    It doesn't even check if its up to date lmfao!
     
  6. Immense

    Immense Developer

    Messages:
    258
    Ratings:
    183
    Go to your thread you made on asking about this, I replied and wrote a whole thing for you on auto updating (code) if need be I can post it in here so others can see it as well. FuzzyGaming45
     
  7. OP
    Chris7S

    Chris7S Nerd by definition, programmer by trade

    Messages:
    1,508
    Ratings:
    479
    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();
    }
    }
    
     
    • Like Like x 1
  8. Christin Sugnan

    Christin Sugnan Newbie

    Messages:
    12
    Ratings:
    0
    File is not replacing if download is completed[DOUBLEPOST=1510052763][/DOUBLEPOST]File is not replacing after Download ![DOUBLEPOST=1510052831][/DOUBLEPOST]

    This code is not working !
     
  9. Immense

    Immense Developer

    Messages:
    258
    Ratings:
    183
    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: Nov 7, 2017
  10. Christin Sugnan

    Christin Sugnan Newbie

    Messages:
    12
    Ratings:
    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.
     
  11. Immense

    Immense Developer

    Messages:
    258
    Ratings:
    183
    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.
     
  12. Christin Sugnan

    Christin Sugnan Newbie

    Messages:
    12
    Ratings:
    0
    Bro whatsapp me +918790684790 I wanna talk wid u . Tq
     
  13. Immense

    Immense Developer

    Messages:
    258
    Ratings:
    183
    Who uses that? Also you can dm me or message my Skype.
     
  14. Christin Sugnan

    Christin Sugnan Newbie

    Messages:
    12
    Ratings:
    0
    Bro I don't have skype account.

    I have whatsapp
     
  15. Immense

    Immense Developer

    Messages:
    258
    Ratings:
    183
    I also presented another option to you which would be by sending me a dm.
     
  16. Christin Sugnan

    Christin Sugnan Newbie

    Messages:
    12
    Ratings:
    0
    Atleast gimme ur email bro so I can keep contact wid you
     
  17. Immense

    Immense Developer

    Messages:
    258
    Ratings:
    183
    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.
     
  18. Christin Sugnan

    Christin Sugnan Newbie

    Messages:
    12
    Ratings:
    0
    I want help !

    My question is. After downloading my app from webclient. How to replace it with new app ?
     
  19. Immense

    Immense Developer

    Messages:
    258
    Ratings:
    183
    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.
     
  20. Christin Sugnan

    Christin Sugnan Newbie

    Messages:
    12
    Ratings:
    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
     

Share This Page