C # - Add File as Resource in Exe at Runtime

I found that I could add Files (* .jpg) to my C # resources in Visual Studio 2010. As far as I could read, it should be possible to rebuild the exe at runtime. I don't want to deprive myself of a separate part. I am looking for C # routines that do this for me. Of course I don't want to modify the executable exe, but a copy of it. I could also live with it by putting my source code inside my exe if I need to compile it again at runtime.

My goal:

  • Create a copy of the executable exe
  • Add the file to this copy somehow .
  • Close the running application
  • When the user makes a copy, he must have the file as a resource inside. It's him.

Edit: The C # + Visual Studio 2010 compiler is available on the target system.


(I am not programming a full installation, please don't say these bad words: "reinvent" and "wheel", I know only one)
+3


source to share


1 answer


It's very difficult for you to change the resource section in such a way that it doesn't break your executable without the benefit of the full compiler.

Instead, you can:

  • Create a ZIP file (or another archive containing all your files).
  • Dumb-append the contents of your ZIP file at the end of the executable
  • Also add an int32 containing the length of your archive.

You can read it by opening it FileStream

to your own executable starting at ExecutableLength - ZipLength - 4

and reading ZipLength

bytes - which only gives you the zip portion that can be read with DotNetZip or another library.



Then when you want to change the saved data:

  • Rename the existing executable at runtime (which you can do)
  • Read the first ExecutableLength - ZipLength - 4

    bytes of your executable and write them to a new file with the name that your executable was originally before it was renamed.
  • Create a new ZIP archive with your information and add it.
  • Add int32 lengths to your new archive.
  • Close the existing application and start it.

Tadah is an executable file that can modify its own stored resources.

+1


source







All Articles