Save data in executable

2019-03-15 23:26发布

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.

7条回答
We Are One
2楼-- · 2019-03-16 00:21

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
查看更多
登录 后发表回答