Can I replace a .NET EXE in the App Directory while the application is still in memory??

A

Aaron Ackerman

I have an updater that I have built and I need to replace a WinForms EXE
from the WinForms app itself. Can I do this while the EXE is in memory or if
not is there a work around?
 
J

Jeremy Williams

Aaron Ackerman said:
Yeah I looked at that it really doesn't answer my question.



http://msdn.microsoft.com/library/default.asp?url=/msdnmag/issues/03/02/bits

Can you have more than one instance of your app running at a time? If so,
then you could try the following:

1) Have the EXE rename its own file (example: MyApp.EXE becomes MyApp.BAK).
This can be accomplished with the System.IO.File.MoveTo method.

2) Copy the new EXE to the app's directory. Since you renamed the original
file, this copy should now work.

3) Launch the new EXE with the System.Diagnostics.Process.Start method

In C#, this might look something like this:

//------------------------------------------------
// Renames the current file
File.Move(@"RenameTest.exe", @"RenameTest.bak");

// Change the next line to get the new file from it's real location (such as
a network folder or a URL)
File.Copy(@"C:\RenameTest.exe", @"RenameTest.exe");

// Starts the app as a new instance
Process.Start("RenameTest.exe");
//------------------------------------------------

I tried this in a console app, and all seemed to go well. You might need to
put in code to clean up the backup file if it exists.

Usually, the kind of process is handled by a bootstrap .exe. You create a
small app that handles copying over new versions of the main app . This
small bootstrap app is the one that you launch each time you want to execute
the main program.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top