C#存储Dat文件并序列化与反序列化
来源:互联网 发布:数据备份解决方案 编辑:程序博客网 时间:2024/05/18 04:01
using System; private void button1_Click(object sender, EventArgs e) Serialize(); #region 序列化 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); catch (SerializationException e) throw; } #region 反序列化 void Deserialize() addresses = (Hashtable)formatter.Deserialize(fs); throw; // To prove that the table deserialized correctly, display the key/value pairs. }
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();
}
{
Deserialize();
}
void Serialize()
{
Hashtable addresses = new Hashtable();
// Construct a BinaryFormatter and use it to serialize the data to the stream.
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(fs, addresses);
}
{
MessageBox.Show("Failed to serialize. Reason: "+e.Message);
finally
{
fs.Close();
}
}
#endregion
{
// 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.
}
catch (SerializationException e)
{
MessageBox.Show("Failed to deserialize. Reason: " + e.Message);
}
finally
{
fs.Close();
}
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文件并序列化与反序列化
- C#生成.dat文件,并序列化与反序列化
- C#生成.dat文件,并序列化与反序列化
- c#序列化与反序列化
- C#序列化与反序列化
- C#序列化与反序列化
- c#序列化 与 反序列化
- C#序列化与反序列化
- C# 序列化与反序列化
- C#序列化与反序列化
- C#序列化与反序列化
- C#序列化与反序列化
- C# 序列化与反序列化
- C#序列化与反序列化
- C# 序列化 与 反序列化
- C#序列化与反序列化
- C# 序列化与反序列化
- C#序列化与反序列化
- 对自身IP数据报在网卡处理的一些认识
- NameValueCollection类用法详解
- socket重用
- RTSP服务器处理客户端点播的基本流程
- 项目团队建设三原则
- C#存储Dat文件并序列化与反序列化
- NameValueCollection类用法详解
- 【转】Linux下获取第一个网络接口名称的函数实现
- com.ctc.wstx.exc.WstxParsingException: Illegal processing instruction target ("xml"); xml (case insensitive) is reserved by the
- HashTable和NameValueCollection的区别示例
- 伤不起呀 伤不起,有木有呀 有木有
- AOP基本概念
- _TCHAR转int,int转_TCHAR
- Flex4中Repeater和Flex3中HorizontalList的用法