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; //need this for file io //********************************************************* // Binary data read/write:: The file will be created if // it does not already exist. The file will be located in // your "project/bin/Release" folder // Edwin Armstrong A.K.A. (efa) //********************************************************* // Date Comment who //========= ======================================= === // 02/22/10 Working and tested version. efa // 02/23/10 Modified to encrypt/decrypt a file. efa //********************************************************* namespace file_io_binary { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //write data button (efa) 02/23/10 private void button1_Click(object sender, EventArgs e) { FileStream fs = File.Create("test.dat"); BinaryWriter bw = new BinaryWriter(fs); byte dataByte; string str; int len = 0; //length of string str = textBox1.Text; len = str.Length; foreach (char letter in str) { dataByte = (byte)letter; dataByte = (byte)(dataByte + 7);// Not much of an encryption, bw.Write(dataByte); // just add 7 to the byte data... } bw.Close(); fs.Close(); } //read data button (efa) 02/23/10 private void button2_Click(object sender, EventArgs e) { string fileName = "test.dat"; char[] buffer = new char[1024]; byte dataByte; int c = 0; if (File.Exists(fileName)) { FileStream fs = File.OpenRead(fileName); BinaryReader br = new BinaryReader(fs); //get the files length System.IO.FileInfo fileInfo = new System.IO.FileInfo(@fileName); // write file length in bytes to textbox3 this.textBox3.Text = fileInfo.Length.ToString(); for (c = 0; c < fileInfo.Length; ++c) { dataByte = br.ReadByte(); dataByte = (byte)(dataByte - 7);// Not much of an decryption, buffer[c] = (char) dataByte; // just subtract 7 from the byte... } string outStr = new string(buffer); textBox2.Text = outStr; br.Close(); fs.Close(); } else { MessageBox.Show("Can't find file [" + fileName + "]"); return; } } } }