Hey, Im new to c# and I was wondering how I would go about making an app that will let me open a file, read a certain offset (say 0x0C) and then have the byte at that offset show up in a textbox. also being able to edit that byte in the textbox and then save the changes with a button.
Help with writing this code would be highly appriciated.
AlanPosted Dec 2, 2008, 3:56 PM
Here's some basic code to do that. I'll leave it to you to add any error handling you need:
using System.IO;
// add these fields to form
private FileStream fs;
private int offset = 0x0C; // or whatever
private string filePath;
// in read button click eventhandler
filePath = "somefile.bin";
fs = File.Open(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
br.BaseStream.Seek(offset,SeekOrigin.Begin);
byte b = br.ReadByte();
br.Close();
textBox1.Text = b.ToString("X2"); // display in hex
// after edit in save button click eventhandler
byte b = Convert.ToByte(textBox1.Text, 16);
fs = File.Open(filePath, FileMode.Open, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
bw.Seek(offset,SeekOrigin.Begin);
bw.Write(b);
bw.Close();