blogjava's web log

blogjava's web log
...

系列化例[导入]


[Serializable]
    
public class MyObject 
    
{
        
public int n1 = 0;
        
public int n2 = 0;
        
public String str = null;
    }

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO ;
using System.Runtime .Serialization .Formatters .Binary ;
using System.Runtime .Serialization ;

//系列化
private void button1_Click(object sender, System.EventArgs e)
        
{
            MyObject obj 
= new MyObject();
            obj.n1 
= 1;
            obj.n2 
= 24;
            obj.str 
= "Some String";
            IFormatter formatter 
= new BinaryFormatter();
            Stream stream 
= new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);
            formatter.Serialize(stream, obj);
            stream.Close();

        }

//反。。。。。。
        private void button2_Click(object sender, System.EventArgs e)
        
{
            IFormatter formatter 
= new BinaryFormatter();
            Stream stream 
= new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
            MyObject obj 
= (MyObject) formatter.Deserialize(stream);
            stream.Close();
            MessageBox.Show (obj.n1.ToString () );

        }

posted on 2006-05-28 14:25 record java and net 阅读(316) 评论(0)  编辑  收藏 所属分类: dot net相关


只有注册用户登录后才能发表评论。


网站导航:
 

导航

常用链接

留言簿(44)

新闻档案

2.动态语言

3.工具箱

9.文档教程

友情链接

搜索

最新评论