[Solved] C# How do I append code to the end of a running .NET .EXE, preferably from inside that .EXE?


Despite all the negativity, there is a clean way to do this:

The way I have found only requires the program be executed on an NTFS drive.

The trick is to have your app copy itself to an alternate stream as soon as it’s launched, then execute that image and immediately close itself. This can be easily done with the commands:

type myapp.exe > myapp.exe:image
forfiles /m myapp.exe /c myapp.exe:image

Once your application is running from an alternate stream (myapp.exe:image), it is free to modify the original file (myapp.exe) and read the data that’s stored within it. The next time the program starts, the modified application will be copied to the alternate stream and executed.

This allows you to get the effect of an executable writing to itself while running, without dealing with any extra files and allows you to store all settings within a single .exe file.

The file must be executed on an NTFS partition, but that is not a big deal since all Windows installations use this format. You can still copy the file to other filesystems, you just cannot execute it there.

solved C# How do I append code to the end of a running .NET .EXE, preferably from inside that .EXE?