FileInfo file = new FileInfo(@"C:\Users\User\Documents\C# Stream Tests\binarywriter.dat");
using (BinaryWriter writer = new BinaryWriter(file.OpenWrite()))
{
string message = "message";
string message2 = "message2";
writer.Write(message);
writer.Write(message2);
}
using (BinaryReader reader = new BinaryReader(file.OpenRead()))
{
MessageBox.Show(reader.ReadInt32().ToString());
MessageBox.Show(reader.ReadString());
}
Car car1 = new Car() { LitersFuel = 10 };
Car car2 = new Car() { LitersFuel = 20 };
Car car3;
FileInfo file1 = new FileInfo(@"C:\Users\User\Documents\C# Stream Tests\CarClass1.dat");
FileInfo file2 = new FileInfo(@"C:\Users\User\Documents\C# Stream Tests\CarClass2.dat");
FileInfo file3 = new FileInfo(@"C:\Users\User\Documents\C# Stream Tests\CarClass3.dat");
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream stream1 = file1.OpenWrite())
using(FileStream stream2 = file2.OpenWrite())
{
formatter.Serialize(stream1, car1);
formatter.Serialize(stream2, car2);
}
Byte[] bytes1 = File.ReadAllBytes(file1.FullName);
byte[] bytes2 = File.ReadAllBytes(file2.FullName);
for (int i = 0; i < bytes1.Length; i++)
{
if (bytes1[i] != bytes2[i])
MessageBox.Show("Byte #" + i + " : " + bytes1[i] + " V.S " + bytes2[i]);
}
// Showed Byte #157 : 10 V.S 20
Byte[] bytes3 = bytes1;
bytes3[157] = 30;
File.WriteAllBytes(file3.FullName, bytes3);
using (FileStream stream3 = file3.OpenRead())
{
car3 = formatter.Deserialize(stream3) as Car;
}
MessageBox.Show(car3.LitersFuel.ToString());
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System. IO;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string s1 = "text1";
string s2 = "text2";
SaveFileDialog sfd1 = new SaveFileDialog();
sfd1.Filter = "Textformat|*.rtf";
sfd1.FilterIndex = 0;
sfd1.DefaultExt = "rtf";
if (sfd1.ShowDialog() == DialogResult.OK)
{
using (BinaryWriter binWriter = new BinaryWriter(sfd1.OpenFile()))
{
binWriter.Write(s1);
binWriter.Write(s2);
}
}
FileInfo fi = new FileInfo(sfd1.FileName);
if (fi.Exists == true)
{
using (BinaryReader binReader = new BinaryReader(fi.OpenRead()))
{
string s = Encoding.ASCII.GetString(binReader.ReadBytes((int)binReader.BaseStream.Length));
String[] sArray = s.Split(new String[] { "" }, StringSplitOptions.None);
Console.WriteLine(s);
Console.WriteLine();
foreach (string sArrayResult in sArray)
{
Console.WriteLine(sArrayResult);
}
}
}
Console.ReadLine();
}
}
}
resulting Array row 1 text1 text2 ↑537472696E672054657374 ?☻ ☺ ????☺ ♀☻ JConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null resulting Array row 2 ☺ ↔ConsoleApplication1.classTest☺ testResult☺☻ ♠♥ ▬537472696E672054657374♂ ?♥FEFE01000FFFFFFFF10000000C20004A436F6E736F6C654170706C69636174696F6E312C205665 7273696F6E3D312E302E302E302C2043756C747572653D6E65757472616C2C205075626C69634B65 79546F6B656E3D6E756C6C510001D436F6E736F6C654170706C69636174696F6E312E636C6173735 46573741000A74657374526573756C74120006300016353337343732363936453637323035343635 37333734B00000000000000000000000000000000000000000000000000000000000000000000000 0000 serialized bytes: 01000FFFFFFFF10000000C20004A436F6E736F6C654170706C69636174696F6E312C205665727369 6F6E3D312E302E302E302C2043756C747572653D6E65757472616C2C205075626C69634B6579546F 6B656E3D6E756C6C510001D436F6E736F6C654170706C69636174696F6E312E636C6173735465737 41000A74657374526573756C74120006300016353337343732363936453637323035343635373337 34B000000000000000000000000000000000000000000000000000000000000000000000000000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System. IO;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApplication1
{
class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string s1 = "text1";
string s2 = "text2";
classTest ct = new classTest("String Test");
SaveFileDialog sfd1 = new SaveFileDialog();
sfd1.Filter = "Textformat|*.rtf";
sfd1.FilterIndex = 0;
sfd1.DefaultExt = "rtf";
if (sfd1.ShowDialog() == DialogResult.OK)
{
using (BinaryWriter binWriter = new BinaryWriter(sfd1.OpenFile()))
{
binWriter.Write(s1 + Environment.NewLine);
binWriter.Write(s2 + Environment.NewLine);
binWriter.Write(ct.ToString() + Environment.NewLine);
string result1 = String.Empty;
string result2 = String.Empty;
MemoryStream ms = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, ct);
Byte[] buffer1 = new Byte[(int)ms.Length];
buffer1 = ms.GetBuffer();
result1 = Encoding.ASCII.GetString(buffer1);
foreach(byte b in buffer1)
{
result2 += b.ToString("X");
}
binWriter.Write(result1 + Environment.NewLine);
binWriter.Write("FEFE" + result2);
}
}
FileInfo fi = new FileInfo(sfd1.FileName);
if (fi.Exists == true)
{
using (BinaryReader binReader = new BinaryReader(fi.OpenRead()))
{
string s = Encoding.ASCII.GetString(binReader.ReadBytes((int)binReader.BaseStream.Length));
String[] sArray = s.Split(new String[] { "" }, StringSplitOptions.None);
int row = 1;
foreach (string sArrayResult in sArray)
{
Console.WriteLine();
Console.WriteLine("resulting Array row {0}", row);
Console.WriteLine(sArrayResult);
if (sArrayResul*****ntains("FEFE"))
{
String[] sArraySerialized = sArrayResult.Split(new String[] { "FEFE" }, StringSplitOptions.None);
string sSerialized = sArraySerialized[1];
Byte[] bArraySerialized = new Byte[sSerialized.Length / 2];
for (int i = 0, j = 0; i < sSerialized.Length; i += 2, j++)
{
bArraySerialized[j] = Byte.Parse(sSerialized.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
}
Console.WriteLine("serialized bytes:");
Console.WriteLine(sSerialized);
MemoryStream ms = new MemoryStream(bArraySerialized);
BinaryFormatter formatter = new BinaryFormatter();
try
{
classTest ct2 = formatter.Deserialize(ms) as classTest;
Console.WriteLine("ct2:");
Console.WriteLine(ct2.ToString());
Console.WriteLine("ms buffer:");
Console.WriteLine(Encoding.ASCII.GetString(ms.GetBuffer()));
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
row++;
}
}
}
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
[Serializable]
public class classTest
{
private string testResult;
public classTest(string hexResult)
{
Byte[] byteArray = Encoding.ASCII.GetBytes(hexResult);
testResult = String.Empty;
foreach (byte b in byteArray)
{
testResult += b.ToString("X");
}
}
public override string ToString()
{
return testResult;
}
}
}
Stream fStream = new FileStream( .... );
BinaryWriter writer = new BinaryWriter(fStream);
BinaryReader reader = new BinaryReader(fStream);
//Write to the file...
writer.Write(10);
writer.Write("Hello nigga!");
writer.Flush(); //Must be called.
//Read and display the file...
string[] lines = new string[] { reader.ReadInt32(),
reader.ReadString() };
foreach(string l in lines) Console.WriteLine(l);
Console.Read();