| | |
Reading/Writing Bytes
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2008
Posts: 99
Reputation:
Solved Threads: 1
Image of my problem
http://i31.tinypic.com/2v3ivyq.png
Left = Actual notepad.exe opened in notepad
Right = what i get after i read/write.
Goal - Be able to store notepad.exe between to splits and then when the application that is houses the bytes is run, it reads those bytes and saves them.
I have the reading/writing sussed but it looks to me like the encoding is the problem.
Builder -
Stub to house the bytes.
Hope you guys have some insight into my problem
http://i31.tinypic.com/2v3ivyq.png
Left = Actual notepad.exe opened in notepad
Right = what i get after i read/write.
Goal - Be able to store notepad.exe between to splits and then when the application that is houses the bytes is run, it reads those bytes and saves them.
I have the reading/writing sussed but it looks to me like the encoding is the problem.
Builder -
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace Polyamory { public partial class Form1 : Form { public Form1() { InitializeComponent(); } static string byteString(byte[] myBytes) { ASCIIEncoding enc = new ASCIIEncoding(); return enc.GetString(myBytes); } private void buildButton_Click(object sender, EventArgs e) { try { string sPath = ""; SaveFileDialog saveFile = new SaveFileDialog(); saveFile.FileName = "built.exe"; saveFile.Filter = "Executable Files (*.exe)|*.exe"; if (saveFile.ShowDialog() == DialogResult.OK) sPath = saveFile.FileName; else return; File.Copy(Application.StartupPath + "/Stub.exe", sPath, true); byte[] myFile = File.ReadAllBytes("C:\\notepad.exe"); File.SetAttributes(sPath, FileAttributes.Normal); string mySplit = "<@@ENCEXE@@>"; string myEndSplit = "</@@ENCEXE@@/>"; string sUrl = mySplit + byteString(myFile) + myEndSplit; FileStream myStream = new FileStream(sPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None); BinaryWriter myWriter = new BinaryWriter(myStream); myStream.Position = myStream.Length + 1; myWriter.Write(sUrl); myStream.Close(); myStream.Dispose(); myWriter.Close(); MessageBox.Show("Built."); } catch (Exception err) { MessageBox.Show(err.Message); } } } }
Stub to house the bytes.
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.IO; using System.Net; using System.Diagnostics; using System.Windows.Forms; namespace Polyamorous { class Program { static string GetBetween(string myFile, string strStart, string strEnd) { int iPos, iEnd, lenStart = strStart.Length, startPos = 0; iPos = myFile.IndexOf(strStart, startPos); iEnd = myFile.IndexOf(strEnd, iPos + lenStart); if(iPos != -1 && iEnd != -1) { return myFile.Substring(iPos + lenStart, iEnd - (iPos + lenStart)); } return "error"; } static byte[] stringByte(string myString) { ASCIIEncoding encoding = new ASCIIEncoding(); return encoding.GetBytes(myString); } static string byteString(byte[] myBytes) { ASCIIEncoding enc = new ASCIIEncoding(); return enc.GetString(myBytes); } static void Main() { try { byte[] myFile = File.ReadAllBytes(Application.ExecutablePath); string sData = GetBetween(byteString(myFile), "<@@ENCEXE@@>", "</@@ENCEXE@@/>"); File.WriteAllBytes("wtf.exe", stringByte(sData)); } catch { Environment.Exit(0); } } } }
Hope you guys have some insight into my problem
Do you have to do this at runtime? You could embed notepad.exe in your project. This seems like a bad idea. Also I don't believe you can just append data wherever you want in an executable so are you sure this concept works? I know you can declare a static length struct() in a C/C++ application and read/write to that struct freely as long as you stay <= the structs size but I don't know that this is possible with .NET assemblies.
Another approach instead of bringing the data back in a byte[] array you could hook the StreamReader up directly the StreamWrite and write the data but this would assume you already had an empty slot for the data to do.
Another approach instead of bringing the data back in a byte[] array you could hook the StreamReader up directly the StreamWrite and write the data but this would assume you already had an empty slot for the data to do.
•
•
Join Date: May 2008
Posts: 99
Reputation:
Solved Threads: 1
•
•
•
•
Do you have to do this at runtime? You could embed notepad.exe in your project. This seems like a bad idea. Also I don't believe you can just append data wherever you want in an executable so are you sure this concept works? I know you can declare a static length struct() in a C/C++ application and read/write to that struct freely as long as you stay <= the structs size but I don't know that this is possible with .NET assemblies.
Another approach instead of bringing the data back in a byte[] array you could hook the StreamReader up directly the StreamWrite and write the data but this would assume you already had an empty slot for the data to do.
![]() |
Similar Threads
- reading/writing text files question - HELP! (Python)
- help need on file reading and writing in python (Python)
- Reading Binary Files in VB (Visual Basic 4 / 5 / 6)
- Reading/Writing To Text File Error (C++)
- question about reading from/writing to a stream (C#)
- pipe reading/writing (C++)
- Help with reading and writing from file (C++)
- Reading/Writing Management Program (C++)
- Reading, Writing and Creating a File (C)
- Need help with my console database app, program reading or writing incorrectly (C++)
Other Threads in the C# Forum
- Previous Thread: loading multiple pictureBox in panel
- Next Thread: Descending Order
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# check checkbox client color combobox control conversion cryptographyc#winformsencryption csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ hash httpwebrequest image index input install java keypress label list listbox listener mandelbrot math mouseclick mysql netcfsvcutil.exe operator path photoshop picturebox pixelinversion pixelminversion post programming radians regex remote remoting richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer update upload usercontrol validate validation view visualstudio webbrowser windows winforms wpf xml






