views:

107

answers:

6

I have a portable executable that saves data to a file in the same folder as the executable. Is there any way that I can save data into the executable itself when I close the app?

This maybe weird, but taking the data with me and only have one file for the exe and data would be great.

Would prefer if this was made with C#, but is not a requisite.

+1  A: 

So right now you're using an app.config (and Settings.settings) file? I believe this is the most compact way to save data close to the .exe.

I would highly doubt you can alter the manifest of the .exe, or any other part of it.

Edit: Apparently, there might be some ways after all: http://www.codeproject.com/KB/msil/reflexil.aspx

Bertvan
+1  A: 

You can, by reserving space through the means of using a string resource and pad it out. You need to do a bit of detective work to find out exactly where in the offset to the executable you wish to dump the data into, but be careful, the file will be "in use", so proceed cautiously with that.

tommieb75
See this other posting I made aeons ago... http://stackoverflow.com/questions/1780260/write-a-value-into-pe-file/1780360#1780360
tommieb75
+2  A: 

You cannot modify your own EXE to contain stored data in anything approaching an elegant or compact way. First off, the OS obtains a lock on the EXE file while the application contained within is being run. Second, an EXE comes pre-compiled (into MSIL at least), and modification of the file's source data usually requires recompilation to reset various pointers to code handles, or else a SERIOUS knowledge on a very esoteric level about what you're doing to the file.

The generally-accepted methods are the application config file, a resource file, or some custom file you create/read/modify at runtime, like you're doing now. Two files for an application should not be cause for concern

KeithS
Yeah, I know its not bad the way it is, but with Macs you can copy the executable, that is also a folder, and all your info goes with you. It seems just a minor detail but would like to put it to work.
Artur Carvalho
+1  A: 

There is one way using multiple streams, but only works in NTFS filesystems. NTFS allows you to define alternative "named" streams in one file. The usual content is in the main = unnamed stream. It has something to do with the extra info you can see when you right click a file and check properties.
Unfortunatly C# has no support for multiple streams, but there are open source pojects that can help you.

See this link for a nice wrapper to read and write multiple streams to one single file in C#

Pablo Grisafi
+1  A: 

Alternate data streams might work. By using the ::stream syntax you can create a data stream within your exe and read/write data. Edit: to create/access an alternate data stream, you will use a different filename. Something like: applicAtion.exe:settings:$data this will access a data stream named "settings" within application.exe. To do this you need to add the :settings:$data to the filename when reading or writing to the file. This funtionality is provided by ntfs so it shold work in c# and should work when the application is running.

Additional information is available at: http://msdn.microsoft.com/en-us/library/aa364404(VS.85).aspx

Mike
@Mike: sorry to be pedantic - but you used syntax that resembles C++ right?
tommieb75
I added a better description of alternate data streams. This is a filesystem feature and is not language specific.
Mike
A: 

If you want to take the data with you and only have one file for the exe and data, .zip them into a self-extracting .exe.

Beth