65.9K
CodeProject is changing. Read more.
Home

A simple update method in C#

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.08/5 (11 votes)

Apr 6, 2011

CPOL
viewsIcon

49207

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");
            }