1

I'm having trouble and I think it's because I'm doing something wrong with BinaryWriter. In essence, I'm trying to write over an existing group of images and text strings in a file using BinaryWriter. The code functions and there's no complaints, errors or anything to tell me it's not working as intended. But when it finishes and I re-load the now supposedly edited file, it's showing the old images and text, as if nothing was saved to the file. This is the bit of code that I think should be doing it but isn't doing it.

const Int32 SAVE_OFFSET = 0x457000;
const Int32 SAVE_SIZE = 0x43A929;
bw = new BinaryWriter(File.Open(saveFile, FileMode.Open, FileAccess.ReadWrite));
bw.BaseStream.Seek(SAVE_OFFSET, SeekOrigin.Begin);
var save = File.ReadAllBytes(datFile);
if (save.Length != SAVE_SIZE)
{
MessageBox.Show("File is not the expected size!\nExpected: " + SAVE_SIZE + "\nActual: " + save.Length);
}
bw.Write(File.ReadAllBytes(datFile), 0, SAVE_SIZE);
bw.Close(); 

Everything seems to be working but it just doesn't save to the file. Any ideas?

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • 1
    This code will not fail if *saveFile* does not exist. Not good. Check the return value of the Seek() call. – Hans Passant Aug 08 '23 at 22:12
  • @HansPassant If the file doesn't exist it throws `FileNotFoundException` on open. If it exists and you seek past the end it pads the file with `0`s to the new length. – Corey Aug 08 '23 at 23:33
  • Thanks guys. However, neither of your responses addresses my concern here. I'm still in the testing stage so I know for sure the file exists. It's not padding with extra zeroes. It's just acting as if I didn't do bw.write. And I can't figure out why. – TrojanNemo Aug 09 '23 at 01:09
  • Why are you mixing `File.ReadAllBytes` and `BinaryWriter`? Why not use `File.WriteAllBytes`? – Enigmativity Aug 09 '23 at 02:00
  • Because File.WriteAllBytes seems to be to write all the bytes to a file, without allowing for seeking as to when in the file to start writing or specifying how many bytes to write. My sample code reads all bytes but it does NOT write all bytes, and it seeks to a specific offset before writing. Can that be accomplished with File.WriteAllBytes and I'm just not seeing it? – TrojanNemo Aug 09 '23 at 02:33
  • @TrojanNemo - Sorry, I didn't see the offset when I first looked. – Enigmativity Aug 09 '23 at 02:44
  • Try adding `bw.Flush()`. Does that make a difference? – Jay Buckman Aug 09 '23 at 14:19
  • What is the path you are writing to? Most likely your writes end up in a redirected folder (%LOCALAPPPATH%\VirtualStore) because you don't have permission to write to the real file. – Ben Voigt Aug 09 '23 at 14:29
  • I'm saving to a specific folder, wherever the original file is from. The new file is written. The changes are not saved in the file. – TrojanNemo Aug 09 '23 at 20:45

1 Answers1

0

when creating your binary writer try to specify fileMode.OpenOrCreate

using var writer = new BinaryWriter(File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read));
writer .Seek(0, SeekOrigin.Begin);
writer.Write(bytes);
hak
  • 49
  • 3
  • That resulted in the same outcome as before, after I modified it as necessary since I have to seek to a specific offset and I have to specify the amount of bytes to be written. – TrojanNemo Aug 09 '23 at 02:45