Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename a running executable (exe) file [duplicate]

We are trying to push updates to multiple servers at once and my manager has found that it is possible to rename running .exe file. Using that knowledge he wants to rename a running exe and copy over a new version of said exe such that anyone running their in memory copy of foo.exe are fine and anybody who opens a shortcut pointing to foo.exe will get a new copy with updates applied.

I guess I need to clarify, He doesn't expect the old copy to magically update, he just expects them to keep running the old copy until they open the exe again, in which case it will then open the new one that has the name of the old one.

It sometimes throws an exception that the file is in use on his program but if he tries renaming it in a loop it will eventually succeed. On my machine I have yet to be able to get it to work even in a loop.

My first and main question is this: Is it ever acceptable to do this. Should renaming a running executable ever be a valid scenario?

Secondly, if it is a valid scenario then how could one reliably do this? Our current thoughts are try a bunch of times using File.Move (C#) to do a rename and if it doesn't work then write out to an error log so it can be handled manually.

like image 745
Astin Avatar asked Jul 14 '11 15:07

Astin


People also ask

Can you rename exe files?

Of course you can, but this really only works for stand alone exes. Anything that gets referenced by dlls, or puts down roots in AppData, or writes to the registry, or......etc. etc.

How rename exe in C++?

You have to make a copy of the executable file. Then you can rename that file and when you close your current program file you need to transfer control to another program preferably a batch file which deleted the original executable. in windows system() should be able to do these stuff.

Can I copy exe files?

Typically, the answer is no. to another machine will typically not work. copied just the executable file.

How do I copy an executable file in Windows?

Create a new program, and you can use "FileCopy" to copy the file to all the locations. Its better if this program is run in Server. and in all the machines, the Destination Folder should be shared with Read/Write access..


3 Answers

An airplane mechanic and a surgeon meet in a bar. The mechanic says "you know, we have basically the same job. We take broken stuff out and put new, better parts in." The surgeon says "yeah, but you don't have to keep the plane flying as you're making the repairs!"

Trying to update an application by moving files while the application is running seems about as dangerous as trying to fix an airplane in flight. Possible? Sure. Greatly increased risk of catestrophic crash? Yep.

If the application you are updating is a managed application, consider using ClickOnce Deployment. That way, the next time someone runs the application, if there is a new version available it will be copied down and installed automatically. That's much more safe and pleasant than trying to mess with an application while its still running.

like image 180
Eric Lippert Avatar answered Oct 17 '22 00:10

Eric Lippert


  1. No, this is not acceptable. Do not do this. This is not a valid deployment mechanism. This should have been yours or his first clue:

    It sometimes throws an exception that the file is in use on his program but if he tries renaming it in a loop it will eventually succeed.

    And it won't work, anyway. His theory is quite wrong:

    Using that knowledge he wants to rename a running exe and copy over a new version of said exe such that anyone running their in memory copy of foo.exe are fine and anybody who opens a shortcut pointing to foo.exe will get a new copy with updates applied.

    Specifically, the copy in memory will not be automatically replaced with the new executable just because it has the same name. The reason that you're allowed to rename the executable in the first place is because the operating system is not using the file name to find the application. The original executable will still be loaded, and it will remain loaded until you explicitly unload it and load the new, modified executable.

    Notice how even modern web browsers like Chrome and Firefox with their super fancy automatic, in the background, no one ever notices that they exist, updaters still have to close and relaunch the application in order to apply the updates.

    Don't worry about shooting the messenger here. It's more likely that your customers and your tech support department will shoot you first.

  2. See number 1.

like image 31
Cody Gray Avatar answered Oct 16 '22 23:10

Cody Gray


In our organization, we solved the problem of Updates by having two release folders say EXE_A and EXE_B. We also have a release folder called EXE which only has links ALL of which points to either to EXE_A or EXE_B from which the user runs the applications.

When we publish a new version of the program, we publish it to the folder that is not referenced in the links and then update the links (EXE). In this way, you do not get into exceptions that users are holding the application / assemblies. Also if a user wants to run the updated version, all he need to do is close / re-execute the link in EXE folder.

like image 4
Viv Avatar answered Oct 17 '22 00:10

Viv