Save data in executable

一曲冷凌霜 提交于 2019-12-09 04:45:04

问题


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:


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




回答2:


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.




回答3:


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




回答4:


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#




回答5:


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




回答6:


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.




回答7:


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


来源:https://stackoverflow.com/questions/3661516/save-data-in-executable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!