黑马程序员-数据二进制序列化

              

               ------------------ Windows Phones 7手机开发、.Net培训、期待与您交流! ----------------------

 

 要序列化 必须把需要序列化的类上声明:[Serializable]

 1 [Serializable]
 2     class Person
 3     {
 4         private string name;
 5 
 6         public string Name
 7         {
 8             get { return name; }
 9             set { name = value; }
10         }
11         private string path;
12 
13         public string Path
14         {
15             get { return path; }
16             set { path = value; }
17         }
18     }

 

序列化 数据

 1  string name = textBox1.Text;
 2             string path = textBox2.Text;
 3             Person p = new Person();
 4             p.Name = name;
          //保存文件路径
5 p.Path = path;
          //把数据加载到文件流中
6 using (FileStream fs = new FileStream("config", FileMode.Create)) 7 { 8 BinaryFormatter bf = new BinaryFormatter();
           //序列化数据
9 bf.Serialize(fs, p); 10 }

 

 

序列化 把正常的数据序列化 序列化之后的数据如下

 

 

 

反序列化  把序列化之后的数据反序列化出来

            Person p = new Person();
       //判断文件是否存在
if (File.Exists("config")) {
          //把文件读入文件流中
using (FileStream fs = new FileStream("config", FileMode.Open)) {
            //把数据加载到序列化中 BinaryFormatter bf
= new BinaryFormatter();
           //把数据反序列化出来 p
= bf.Deserialize(fs) as Person; listBox1.Items.Add(p.Name); } }

 

              

               ------------------ Windows Phones 7手机开发、.Net培训、期待与您交流! ----------------------

转载于:https://www.cnblogs.com/jianjiandandan/archive/2013/04/16/3023907.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值