Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#
Tip/Trick

A simple update method in C#

Rate me:
Please Sign up or sign in to vote.
3.08/5 (11 votes)
3 Feb 2012CPOL 47.4K   13   6
A simple update method in C#
This code is in version 1. I hope you understand my English and my code.

This code is very useful to update your application:

using System.Windows.Forms; //because MessageBox.Show();
using System.Net; //because WebClient
using System.IO; //because StreamReader
using System.Diagnostics; //because Process.Start
using System.Thread;

        static void update()
        {
	        StreamReader versionread;
	        Uri url = new Uri(/*insert your url to check if a new version is available here. It has to be an .txt file*/); //string
		Uri updateurl = new Uri(/*insert your url for the latest .exe version here*/); //string
		int intVers = 0;
            WebClient update = new WebClient();
            update.DownloadFileAsync(url,/*insert the name of the .txt file here*/); //string
            while (update.IsBusy)
            {
                Thread.Sleep(1);
            }
                versionread = new StreamReader(/*insert the name of the .txt file here*/); //string
                intVers = Convert.ToInt32(versionread.ReadLine());

            if (intVers > /*latest version*/)
            {
                update.DownloadFileAsync(updateurl, /*insert the name of the latest .exe version here*/); //string
                while (update.IsBusy)
                {
                    Thread.Sleep(1);
                }

                    MessageBox.Show("Update sucessfully");
                    //
					//installing process
					//
					Process.Start(/*insert the name of the latest .exe version here*/); //string
                    Close();
            }
            else
            {
                MessageBox.Show("No Version available");
            }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAlthough it isn't strictly an issue in C#, it's ill advised ... Pin
StephenPhillips6-Feb-12 12:51
StephenPhillips6-Feb-12 12:51 
GeneralTwo tips: 1. You should either use the webclient in a 'using... Pin
Groulien14-Apr-11 3:07
Groulien14-Apr-11 3:07 
Two tips:
1. You should either use the webclient in a 'using' context or call Dispose().
2. Versions usually aren't integers but a Version[^].
It might not support old versions but the title did say 'simple'.
Voting 4.
GeneralReason for my vote of 3 Very rough code, needs better loopin... Pin
Trellium13-Apr-11 17:46
Trellium13-Apr-11 17:46 
GeneralReason for my vote of 3 My biggest complain is that, in gene... Pin
Ivan Ičin12-Apr-11 13:46
Ivan Ičin12-Apr-11 13:46 
GeneralReason for my vote of 3 Never have endless loop that do noth... Pin
Kim Togo11-Apr-11 22:22
professionalKim Togo11-Apr-11 22:22 
GeneralReason for my vote of 3 1. Endless cycles? Brrrrr 2. Can you... Pin
intrueder7-Apr-11 14:32
intrueder7-Apr-11 14:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.