//1.普通写法
//2.忽略字节的写法
//3.忽略转换的写法
//2.忽略字节的写法
//3.忽略转换的写法
using (Stream stream = new MemoryStream())
{
byte[] bytes1 = UnicodeEncoding.Unicode.GetBytes(s);
byte[] bytes2 = BitConverter.GetBytes(n);
stream.Write(bytes1, 0, bytes1.Length);
stream.Write(bytes2, 0, bytes2.Length);
stream.Seek(0, SeekOrigin.Begin);
byte[] bytes3 = new byte[stream.Length - 4];
byte[] bytes4 = new byte[4];
stream.Read(bytes3, 0, bytes3.Length);
stream.Read(bytes4, 0, bytes4.Length);
MessageBox.Show(UnicodeEncoding.Unicode.GetString(bytes3)+" "+BitConverter.ToInt32(bytes4,0));
//====================================================
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine(s);
writer.WriteLine(n);
writer.Flush();
stream.Seek(0, SeekOrigin.Begin);
StreamReader reader = new StreamReader(stream);
string s2 = reader.ReadLine();
int n2 = int.Parse(reader.ReadLine);
//=========================================================
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(s);
writer.Write(n);
writer.Flush();
stream.Seek(0,SeekOrigin.Begin);
BinaryReader reader = new BinaryReader(stream);
string s2 = reader.ReadString();
int n2 = reader.ReadInt32();
}
{
byte[] bytes1 = UnicodeEncoding.Unicode.GetBytes(s);
byte[] bytes2 = BitConverter.GetBytes(n);
stream.Write(bytes1, 0, bytes1.Length);
stream.Write(bytes2, 0, bytes2.Length);
stream.Seek(0, SeekOrigin.Begin);
byte[] bytes3 = new byte[stream.Length - 4];
byte[] bytes4 = new byte[4];
stream.Read(bytes3, 0, bytes3.Length);
stream.Read(bytes4, 0, bytes4.Length);
MessageBox.Show(UnicodeEncoding.Unicode.GetString(bytes3)+" "+BitConverter.ToInt32(bytes4,0));
//====================================================
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine(s);
writer.WriteLine(n);
writer.Flush();
stream.Seek(0, SeekOrigin.Begin);
StreamReader reader = new StreamReader(stream);
string s2 = reader.ReadLine();
int n2 = int.Parse(reader.ReadLine);
//=========================================================
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(s);
writer.Write(n);
writer.Flush();
stream.Seek(0,SeekOrigin.Begin);
BinaryReader reader = new BinaryReader(stream);
string s2 = reader.ReadString();
int n2 = reader.ReadInt32();
}