Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

Can we run a .exe application that download another .exe from server and replace it with the current one ? in c#.
Any examples ?

Thanks in advance.
Posted
Comments
johannesnestler 16-Jul-13 8:22am    
You mean kind of updater for your app (involves version check)?
[no name] 16-Jul-13 8:32am    
Yes something similar
pasztorpisti 16-Jul-13 8:47am    
You can replace the exe if it is prepared for replacement and cooperates with another updater exe. I answered how to do it in a similar topic: Restart application within itself

If the exe is running, you can't replace it without either shutting down the process or restarting the entire machine. That is why it is common practice than an installer asks you to restart after an installation has finished.

I would place the exe somewhere else, and add a job that is executed when the computer is started (restarted) that replaces the old exe with the new.

You can do that using the RunOnce registry key:

http://stackoverflow.com/questions/7483230/how-can-i-run-an-app-automatic-after-restart[^]

As for the file download: Use the WebClient object:

http://msdn.microsoft.com/en-us/library/ez801hhe.aspx[^]

http://www.csharp-examples.net/download-files/[^]
 
Share this answer
 
v3
Comments
[no name] 16-Jul-13 8:39am    
Thanks a lot .
Well actually there is way to replace an executable from itself.
I discovered it years ago.

While you cannot delete the current executing .exe program file, you can move it!!!

So, first I download the update file from my web server to an /updates/myprogram.exe

Then I
1) File.Delete any previous myprogram.bak file
2) File.Move the myprogram.exe to myprogram.bak.
3) File.Copy the /updates/myprogram.exe to /myprogram.exe location.
4) ask the user if they want to restart, calling Application.Restart()

Voila. Problem resolved in your main program. No need to a boot program. The File.Move command is the secret!!! It basically renames the executable to the backup filename.
 
Share this answer
 
Comments
Andrew-Suffolk-UK 26-May-18 18:23pm    
Brilliant - Got this working myself in a formless vb app. Only difficulty was that the previous 'Properties/Details' viewable in Win-Explorer seem to be adopted by the new exe if you do the whole process in one folder - an extra stage (2a) Delete .Exe solved that problem as well.
Member 14821476 10-Aug-22 6:42am    
Works great! Thanks a lot!
it's not possible to replace the application while it is running.
but u can intimate your system to replace it when it will start/restart next time...

u can use following API to do this :

[Flags]
enum MoveFileFlags
{
 MOVEFILE_REPLACE_EXISTING = 0x00000001,
 MOVEFILE_COPY_ALLOWED = 0x00000002,
 MOVEFILE_DELAY_UNTIL_REBOOT = 0x00000004,
 MOVEFILE_WRITE_THROUGH = 0x00000008,
 MOVEFILE_CREATE_HARDLINK = 0x00000010,
 MOVEFILE_FAIL_IF_NOT_TRACKABLE = 0x00000020
}

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, MoveFileFlags dwFlags);



C#
// How to use

MoveFileEx(__source, __destination, MoveFileFlags.MOVEFILE_DELAY_UNTIL_REBOOT | MoveFileFlags.MOVEFILE_REPLACE_EXISTING);
 
Share this answer
 
Comments
johannesnestler 17-Jul-13 3:37am    
good to know - thanks.
Hi jokler.007,

I think Johnny J.'s answer is good and helps you with the involved steps.

It depends on your needs what you have to do to get a good user experience. Consider not rolling your own solution but use prepared deployment solutions like ClickOnce which have updater included.

If you want to create your own "System" it may help to hear what I did:

Updater.exe is started out of my Target.exe (a Windows application)
Updater.exe terminates Target.exe (or waits for Termination if Target.exe closed itself)
Updater.exe searches for possible updates on (configurabale) update sources (local, Network, Internet)
If update sources are found Updater.exe does a Version check - if a newer Version is available.
If newer Version was found download/copy as new Target.exe
Updater.exe starts Target.exe again and closes itself.

Of course a lot of other "routs" could be taken - just an example.

My solution had some key points - maybe the code helps/inspires you:

Wait for process to Exit

C#
static bool WaitForProcessExit(string strProcessName, int iWaitTime)
        {
            Process[] aProcs = Process.GetProcessesByName(strProcessName);
            // Ist der Prozess noch aktiv?
            if (aProcs != null && aProcs.Length > 0)
            {
                // ... Wenn ja, auf Beenden warten.
                return aProcs[0].WaitForExit(iWaitTime);
            }

            return true;
        }


compare assembly versions of files to update without loading them:

C#
static Version GetVersionFromAssemblyFullName(string strAssemblyPath)
 {
     string strVersion = AssemblyName.GetAssemblyName(strAssemblyPath).FullName.Split(",".ToCharArray())[1].Split("=".ToCharArray())[1];
     return new Version(strVersion);
 }


C#
Version versionNew = GetVersionFromAssemblyFullName(strNewAssemblyPath);
                            Version versionOld = GetVersionFromAssemblyFullName(strCurrentAssemblyPath);

                            if (versionNew > versionOld)
...


greetings Johannes
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900