using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace InstallShieldBranch { public partial class BrancherForm : Form { private readonly BackgroundWorker _worker; public BrancherForm() { InitializeComponent(); _worker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true }; _worker.DoWork += worker_DoWork; } void worker_DoWork(object sender, DoWorkEventArgs e) { var searchAndReplace = new List>() { //Tuple.Create( // BitConverter.GetBytes((UInt32)0x5f544E49), //_TNI // BitConverter.GetBytes((UInt32)0x5f544E49)), //_TNI //Tuple.Create( // BitConverter.GetBytes((UInt32)0x31305f32), //10_2 // BitConverter.GetBytes((UInt32)0x32305f32)), //20_2 Tuple.Create( BitConverter.GetBytes((UInt32)0x5f564544), //_VED BitConverter.GetBytes((UInt32)0x5f564544)), //_VED Tuple.Create( BitConverter.GetBytes((UInt32)0x35305f32), //50_2 BitConverter.GetBytes((UInt32)0x30305f33)), //00_3 }; using (var reader = new BinaryReader(new FileStream(@"C:\Projects\TestSetups\Installer_DataPRO_x64_DEV_2_05.ise", FileMode.Open))) { using (var writer = new BinaryWriter(new FileStream(@"C:\Projects\TestSetups\Installer_DataPRO_x64_DEV_3_00 - New.ise", FileMode.Create))) { BinaryUtility.Replace(reader, writer, searchAndReplace); } } MessageBox.Show("Done"); } public static class BinaryUtility { public static IEnumerable GetByteStream(BinaryReader reader) { const int bufferSize = 1024; byte[] buffer; do { buffer = reader.ReadBytes(bufferSize); foreach (var d in buffer) { yield return d; } } while (bufferSize == buffer.Length); } public static void Replace(BinaryReader reader, BinaryWriter writer, IEnumerable> searchAndReplace) { foreach (byte d in Replace(GetByteStream(reader), searchAndReplace)) { writer.Write(d); } } public static IEnumerable Replace(IEnumerable source, IEnumerable> searchAndReplace) { foreach (var s in searchAndReplace) { source = Replace(source, s.Item1, s.Item2); } return source; } public static IEnumerable Replace(IEnumerable input, IEnumerable from, IEnumerable to) { var fromEnumerator = from.GetEnumerator(); fromEnumerator.MoveNext(); int match = 0; foreach (var data in input) { if (data == fromEnumerator.Current) { match++; if (fromEnumerator.MoveNext()) { continue; } foreach (byte d in to) { yield return d; } match = 0; fromEnumerator.Reset(); fromEnumerator.MoveNext(); continue; } if (0 != match) { foreach (byte d in from.Take(match)) { yield return d; } match = 0; fromEnumerator.Reset(); fromEnumerator.MoveNext(); } yield return data; } if (0 != match) { foreach (byte d in from.Take(match)) { yield return d; } } } } private void button1_Click(object sender, EventArgs e) { _worker.RunWorkerAsync(); } } }