I want to make a method that takes any file and reads it as an array of 0s and 1s, i.e. its binary code. I want to save that binary code as a text file. Can you help me? Tha
Generally, I don't really see a possible way to do this. I've exhausted all of the options that the earlier comments gave you, and they don't seem to work. You could try this:
`private void button1_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "This PC\\Documents";
openFileDialog1.Filter = "All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.Title = "Open a file with code";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string exeCode = string.Empty;
using (BinaryReader br = new BinaryReader(File.OpenRead(openFileDialog1.FileName))) //Sets a new integer to the BinaryReader
{
br.BaseStream.Seek(0x4D, SeekOrigin.Begin); //The seek is starting from 0x4D
exeCode = Encoding.UTF8.GetString(br.ReadBytes(1000000000)); //Reads as many bytes as it can from the beginning of the .exe file
}
using (BinaryReader br = new BinaryReader(File.OpenRead(openFileDialog1.FileName)))
br.Close(); //Closes the BinaryReader. Without it, opening the file with any other command will result the error "This file is being used by another process".
richTextBox1.Text = exeCode;
}
}`
That's the code for the "Open..." button, but here's the code for the "Save..." button:
` private void button2_Click(object sender, EventArgs e) { SaveFileDialog save = new SaveFileDialog();
save.Filter = "All Files (*.*)|*.*";
save.Title = "Save Your Changes";
save.InitialDirectory = "This PC\\Documents";
save.FilterIndex = 1;
if (save.ShowDialog() == DialogResult.OK)
{
using (BinaryWriter bw = new BinaryWriter(File.OpenWrite(save.FileName))) //Sets a new integer to the BinaryReader
{
bw.BaseStream.Seek(0x4D, SeekOrigin.Begin); //The seek is starting from 0x4D
bw.Write(richTextBox1.Text);
}
}
}`
That's the save button. This works fine, but only shows the '!This cannot be run in DOS-Mode!' - Otherwise, if you can fix this, I don't know what to do.
My Site