开发者

Save data in executable

开发者 https://www.devze.com 2023-01-15 18:22 出处:网络
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?

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开发者_如何学Go be great.

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


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


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.


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


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#


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 functionality 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


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.


you can add data to end of executable file :

var executableName = Process.GetCurrentProcess().MainModule.FileName;

    // rename executable file
    var newExecutableName = fullPath.Replace(".exe", "_.exe");
    FileInfo fi = new FileInfo(executableName);
    fi.MoveTo(newExecutableName);

    // make copy of executable file to original name
    File.Copy(newExecutableName, executableName);

    // write data end of new file
    var bytes = Encoding.ASCII.GetBytes("new data...");

    using (FileStream file = File.OpenWrite(executableName))
      {
          file.Seek(0, SeekOrigin.End);
          file.Write(bytes, 0, bytes.Length);
       }

    // we can delete old file when exited
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号