我有两个应用程序需要以快速的方式相互传递值,并且需要保留一些值(当我重新启动计算机时它仍然存在),所以我需要创建一个文件,现在我知道如何与int
:
using (BinaryWriter writer = new BinaryWriter(new FileStream(@"C:\TEST", FileMode.Open)))
{
writer.Write(0); //00 00 00 00
writer.Write(1); //01 00 00 00
writer.Write(2); //02 00 00 00
writer.Write(3); //03 00 00 00
writer.Write(int.MaxValue); //FF FF FF 7F
}
byte[] test = new byte[4];
using (BinaryReader reader = new BinaryReader(new FileStream(@"C:\TEST", FileMode.Open)))
{
reader.BaseStream.Seek(8, SeekOrigin.Begin);
reader.Read(test, 0, 4);
Console.WriteLine(BitConverter.ToInt32(test, 0)); //2
reader.BaseStream.Seek(16, SeekOrigin.Begin);
reader.Read(test, 0, 4);
Console.WriteLine(BitConverter.ToInt32(test, 0)); //2147483647
Console.Read();
}
但如何处理double
?
答案 0 :(得分:1)
就像
一样简单writer.Write((double)int.MaxValue);
Write(Double)将一个8字节的浮点值写入 当前流并将流位置提前八个字节
至于阅读
reader.ReadDouble()