Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Auto Updating the exe from a network location when application starts

0.00/5 (No votes)
27 Jan 2015 1  
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

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here