using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Collections;
namespace test{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Serialize();
Deserialize();
}
#region 序列化
void Serialize()
{
Hashtable addresses = new Hashtable();
addresses.Add("liugognxun", "1");
addresses.Add("ecmastery", "2");
addresses.Add("jakemary", "3");
// To serialize the hashtable and its key/value pairs,
// you must first open a stream for writing.
// In this case, use a file stream.
FileStream fs = new FileStream("D:\\a.dat", FileMode.Create);
// Construct a BinaryFormatter and use it to serialize the data to the stream.
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(fs, addresses);
}
catch (SerializationException e)
{
MessageBox.Show("Failed to serialize. Reason: "+e.Message);
throw;
}
finally
{
fs.Close();
}
}
#endregion
#region 反序列化
void Deserialize()
{
// Declare the hashtable reference.
Hashtable addresses = null;
// Open the file containing the data that you want to deserialize.
FileStream fs = new FileStream("D:\\a.dat", FileMode.Open);
try
{
BinaryFormatter formatter = new BinaryFormatter();
// Deserialize the hashtable from the file and assign the reference to the local variable.
addresses = (Hashtable)formatter.Deserialize(fs);
}
catch (SerializationException e)
{
MessageBox.Show("Failed to deserialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();
}
// To prove that the table deserialized correctly, display the key/value pairs.
foreach (DictionaryEntry de in addresses)
{
// MessageBox.Show("{0} lives at {1}.",de.Key, de.Value);
MessageBox.Show(de.Key.ToString() + "--" +de.Value.ToString()));
}
}
#endregion
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
C#生成.dat文件
共3个文件
txt:2个
exe:1个
5星 · 超过95%的资源 需积分: 49 132 下载量 27 浏览量
2013-06-09
07:55:38
上传
评论 2
收藏 7KB RAR 举报
温馨提示
C#生成.dat文件,并序列化与反序列化源码,可参考
资源推荐
资源详情
资源评论
收起资源包目录
C#生成.dat文件,并序列化与反序列化 .rar (3个子文件)
C#生成.dat文件,并序列化与反序列化 .txt 3KB
更新说明.txt 152B
网络信息采集发布工具V7.0最新版.exe 12KB
共 3 条
- 1
xieyongbao
- 粉丝: 9
- 资源: 29
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页