There is a file 3.exe, in which 2 more files are written: 1.ehe and 2.ehe (added to the end byte-by-bye), you need to “pull out” separately 1.ehe and 2.ehe, that's the whole complexity. 1.ehe can be obtained in this way: starting from copying from the weight of 3.ehe without these 2 files and before the beginning of 2.ehe, copying 2.ehe is similar. But then the whole difficulty, how to understand where 2. ehe begins? there was an idea to write to the end of the file the size of 3.ehe without these 2 files + the size of 1.ehe, thereby get the beginning of the file 2.ehe, but the maximum value is 255 and nothing happened, it is written 0. Can you suggest how best to implement?

I hope that I have explained clearly, if not, then write, I will try differently.

Code example:

var fil2 = File.ReadAllBytes(@"2.exe"); var fil1 = File.ReadAllBytes(@"1.exe"); using (FileStream fs = new FileStream(@"bin.exe", FileMode.Append, FileAccess.Write)) { fs.Write(fil2, 0, fil2.Length); fs.Flush(); fs.Close(); } var rezd = new byte[4] { 5, 55, 56, (byte)fil2.Length }; using (FileStream fs = new FileStream(@"bin.exe", FileMode.Append, FileAccess.Write)) { fs.Write(fil1, 0, fil1.Length); fs.Write(rezd, 0, rezd.Length); fs.Flush(); fs.Close(); } 
  • I would look for the signature of the exe file. - Qwertiy ♦
  • one
    "... I hope that I have explained it in an accessible way ..." - how to say it more softly - in short, Ivan Bezdomny appeared before my eyes, writing an explanation of how the tram to Berlioz snapped his head off. - Alexander Muksimov
  • Well, since you yourself write, you read it yourself, then at the beginning of the file you can organize a block of fixed length, in which the number of files and their length are recorded. - Alexander Muksimov
  • "but the maximum value is 255 and nothing happened" - explain this pearl in more detail - Igor
  • @Qwertiy can you give an example? - Lolidze

1 answer 1

You need to use BinaryWriter on top of the file stream. Not quite sure what you have there for bytes in rezd , but overall the code will look something like this:

 var fil2 = File.ReadAllBytes(@"2.exe"); var fil1 = File.ReadAllBytes(@"1.exe"); using (var fs = new FileStream(@"bin.exe", FileMode.Append, FileAccess.Write)) { fs.Write(fil2, 0, fil2.Length); } var rezd = new byte[] { 5, 55, 56 }; using (var fs = new FileStream(@"bin.exe", FileMode.Append, FileAccess.Write)) { using (var writer = new BinaryWriter(fs)) { writer.Write(fil1); writer.Write(rezd); writer.Write(fil2.Length); } } 

You use using , so manual Flush / Close calls are not needed.

  • Thanks, rezd is purely for me, so I oriented myself in the hex editor. Now 4 bytes were written to the end of the file (after rezd) 0 108 0 0 (looked at char dec), length fil2.Length = 27648 - Lolidze
  • and how do i get exactly 27648? I also need to know where to copy the bytes, from where it is - Lolidze
  • one
    @Lolidze while reading - unwind the reading stream to the position (total length -4), open the BinaryReader on top of it, and read the 4 bytes from there by calling ReadInt32 - PashaPash ♦
  • yes, thank you, everything turned out - Lolidze
  • I didn’t think about it :( how to get access to my own resources when the program is open? BinaryReader fss = new BinaryReader (File.Open (System.Reflection.Assembly.GetExecutingAssembly (). Location, FileMode.Open)); - Lolidze