Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

A simple update method in C#

3.08/5 (11 votes)
3 Feb 2012CPOL 49.1K  
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)