可参考雨松大神:
http://www.xuanyusong.com/archives/1919
http://www.xuanyusong.com/archives/1901
文章将实现数据存储为二进制文件,然后通过二进制文件解析数据。
二进制文件读写操作:
//写入二进制文件
public static void WriteByteValues()
{
string fileName = @"C:\Test.dat"
if (File.Exists(fileName))
File.Delete(fileName);
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(1.250F);
bw.Write(@"c:\Test");
bw.Write(10);
bw.Write(true);
bw.Close();
fs.Close();
}
//读取二进制文件
public static void ReadByteValues()
{
if (File.Exists(fileName))
{
string fileName = @"C:\Test.dat"
FileStream fs = new FileStream(fileName, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
Console.WriteLine(br.ReadSingle());
Console.WriteLine(br.ReadString());
Console.WriteLine(br.ReadInt32());
Console.WriteLine(br.ReadBoolean());
fs.Close();
br.Close();
}
}
测试:
static void Main(string[] args)
{
WriteByteValues();
ReadByteValues();
Console.ReadKey();
}
二进制序列化和反序列化操作:
[Serializable]
public class Car
{
private Radio radio;
public string CarName { get; set; }
public string OwnerOfCar { get; set; }
}
[Serializable]
public class ChineseCar : Car
{
public int MaxSpeed { set; get; }
public