C# 读写自定义二进制文件格式
class myUSER
{
public byte ID { get; set; }
public string user_name { get; set; }
public string name_pass { get; set; }
public bool guest { get; set; }
}
myUSER myuser = new myUSER();
myuser.ID = 101;
myuser.user_name = "XXXXXX";
myuser.name_pass = "123456";
myuser.guest = false;
byte[] data = new byte[42];
data[0] = myuser.ID;
Array.Copy(System.Text.Encoding.Unicode.GetBytes(myuser.user_name), 0, data, 1, myuser.user_name.Length*2);
Array.Copy(System.Text.Encoding.Unicode.GetBytes(myuser.name_pass), 0, data, 21,myuser.name_pass.Length*2);
data[41] = Convert.ToByte(myuser.guest);
try
{
FileStream fs=File.Open(Application.StartupPath + "\\wljs.bin",FileMode.Create,FileAccess.ReadWrite);
fs.Write(data, 0,42);
fs.Flush();
fs.Close();
}
catch
{
}
能写入到文件中,但如果要进行插入和删除应该怎么办?谢谢!删除我想可做个删除标志后不去读它,但插入呢?难道每次都要建个临时文件来导一次?初学编程,请指教!谢谢了!