标签:bsp ble style gen 创建序列 .text vat 打开 creat
将文件保存序列化至文档中,然后再读取:
//首先创建可序列化的实体类
[Serializable] public class Message { public string Name { get; set; } public int Age { get; set; } public DateTime Time { get; set; } public char Gender { get; set; } }
下面是序列化保存文件,以及反序列化读取文件
1 private void btn_Click(object sender,EventArgs e){ 2 3 Message ms=new Message() 4 5 { 6 Name=this.txt_name.Text.trim().tostring(), 7 8 Age=Convert.Toint32(this.txt_age.Text.trim()), 9 10 Time=Convert.ToDateTime(this.txt_time.text.trim()), 11 12 Gender=Convert.Tochar(this.txt_gender.text.trim()) 13 }; 14 15 this.writerFile(ms); 16 17 } 18 19 private void writeFile(object Message){ 20 //创建序列化文件 21 22 FileStream fs=new FileStream("MessageFileName",FileMode.Create) 23 24 //创建二进制格式化器 25 26 BinaryFormatter bf=new BinaryFormatter(); 27 28 //将对象序列化 29 30 bf.Serialize(fs,Message); 31 32 fs.close(); 33 34 } 35 36 37 38 //反序列化 将文本信息反序列化读取出来 39 40 private void btn_read_Click( Object sender,EventArgs e){ 41 ReadFile(); 42 } 43 44 private void ReadFile(){ 45 //打开序列化文件 46 47 FileStream fs=new FileStream("MessageName",FileMode.Open) 48 49 BinaryFormatter bf=new BinaryFormatter(); 50 51 //将文件反序列化 52 53 Message ms=(Message)bf.Deserialize(fs); 54 55 //进行赋值 56 57 txt_name.text=ms.Name.tostring(); 58 59 fs.Close(); 60 }
标签:bsp ble style gen 创建序列 .text vat 打开 creat
原文地址:https://www.cnblogs.com/skyphs/p/10874704.html