Newbie here. How do I do a find and replace in a binary file? I need
to read in a binary file then replace a string "ABC" with another
string "XYZ" then write to a new file. Find string is the same length
as Replace string. Here's what I have so far. I spent many hours
googling for sample code but couldn't find much. Thanks...
public static void FindReplace(string OldFile, string NewFile)
{
string sFind = "ABC"; //I probably need to convert these
to a byte array
string sReplace = "XYZ"; //but I don't know how.
int i;
FileStream fin = new FileStream(OldFile, FileMode.Open);
FileStream fout = new FileStream(NewFile,
FileMode.Create);
do
{
i = fin.ReadByte();
if (i != -1)
{
//I think I need to compare the byte being read in
//to the 1st sFind byte array here. If it matches
then
//store the position and compare the next byte.
//If all 3 bytes match then replace with sReplace
byte array.
//I'm just not sure how to do it.
fout.WriteByte((byte)i);
}
} while (i != -1);
fin.Close();
fout.Close();
}