Click here to Skip to main content
15,868,164 members
Articles / Desktop Programming / WPF
Tip/Trick

Auto Updating the exe from a network location when application starts

Rate me:
Please Sign up or sign in to vote.
5.00/5 (13 votes)
27 Jan 2015CPOL1 min read 23.3K   22   6
Auto updating the exe if the latest exe is available in the network share

Introduction

 This article consist  of a a console application which will help the developer or implementor from individually going to each user and updating the exe when there is a updating or when there is a latest version build available

Background

I  was developing an WindowsForm application and when I feel that I had achieved all the requirements I gave the exe for all the users. Since its a small app I had not used any install wizard or deployment steps I just simply copied the exe file from the debug folder of the project and had given to the users. but all of a suddenly more requirements came from the users and I was forced to in cooperate them also and the biggest problem faced by me is to replacing the old exe of the users with the new one each time when I make a new build.so I decide to write a simple code to check a network folder whether there is a updated exe and replace the old exe with the new one.

Using the code

I wrote a simple console application in c# to accomplish this task

In the Program.cs itself I wrote the code to check updates and then executed the updated application

C#
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShipitIntro
{
    class Program
    {
        static void Main(string[] args)
        {
         //updatepath is the location where I upload updated exe 
            string UpdatePath = @"\\testserver\Art\ship it\Shipit.exe";
          //applocation is the location from where this console app runs.It will also be the location where the new file will be saved 
            string AppLocation = Directory.GetCurrentDirectory() + @"\shipit.exe";
          
          
          
            try
            {
                FileInfo info1 = null;
                FileInfo info2 = null;
                if (File.Exists(UpdatePath))
                {
              //If there is a file in the update location info1 will get the fileinfo of that file 
                    info1 = new FileInfo(UpdatePath);
                }

                if (File.Exists(AppLocation))
                {
//if the exe is already present in the aplocation get its information also
                    info2 = new FileInfo(AppLocation);

                }
                else
                {
                    File.Copy(UpdatePath, AppLocation, true);
                    ExecuteApp(AppLocation);
                    return;
                }
                if (info1.LastWriteTime > info2.LastWriteTime)
                {
                    File.Copy(UpdatePath, AppLocation, true);
                }

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            ExecuteApp(AppLocation);

        }
        static void ExecuteApp(string location)
        {
            if (File.Exists(location))
            {
                try
                {
                    Process.Start(location);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message); return;
                }
            }
            else
            {
               
            }
        }
       
    }
}

 The  function executeApp() will help in starting the exe from the location .thus it makes sure that the user always use the latest exe

Points of Interest

Now we had taken the last modified date as criteria more control can be attained if we do it using the version of the exe.and thus implementing the version control

History

Thanks for the Help I recieved from the my friend and my senior Mr Roymon for doing this.

License

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


Written By
Software Developer
United Arab Emirates United Arab Emirates
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionClickOnce Pin
Mike Meinz28-Jan-15 10:16
Mike Meinz28-Jan-15 10:16 
AnswerRe: ClickOnce Pin
SREENATH GANGA30-Jan-15 22:32
SREENATH GANGA30-Jan-15 22:32 
GeneralRe: ClickOnce Pin
Mike Meinz31-Jan-15 1:59
Mike Meinz31-Jan-15 1:59 
Suggestionnice start.. Pin
deo cabral27-Jan-15 14:22
professionaldeo cabral27-Jan-15 14:22 
QuestionHandy tip! Pin
B. Clay Shannon27-Jan-15 5:58
professionalB. Clay Shannon27-Jan-15 5:58 
AnswerRe: Handy tip! Pin
SREENATH GANGA28-Jan-15 2:56
SREENATH GANGA28-Jan-15 2:56 
Thankx bro

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.