tstring 试验室

tstring 社区首页 | 简易帐本 | 慢速英语 | 单词查询 | 单机象棋 | 视频播放器 快速登录 | 快速注册 | >>

tstring序列化和反序列化 <-- 返回首页

hebkkun 咕嘀咕嘀哒哟咿
#1
定义一个类,
using System;
using System.Collections;


namespace Tstring
{
///
/// SubjectInfo 的摘要说明。
///

[Serializable]
public class SubjectInfo
{
public SubjectInfo(){}

#region Fields
private string _FileUrl;
private string _FileTitle;
#endregion

#region Properties
public string FileUrl{
get{return _FileUrl;}
set{_FileUrl = value;}
}

public string FileTile{
get{return _FileTitle;}
set{_FileTitle = value;}
}
#endregion
}

[Serializable]
public class SubjectCollect : CollectionBase{
public void Add(SubjectInfo item){
List.Add(item);
}

public void Remove(SubjectInfo item){
List.Remove(item);
}

public SubjectInfo this[int i]{
get{return (SubjectInfo)List[i];}
set{List[i] = value;}
}
}
}



while (reader.Read()) {
j++;
SubjectInfo info = new SubjectInfo();
string file = path + reader["FileURL"].ToString().Replace("/",@"\");
this.label1.Text = string.Format("> {0}",file);
if(!System.IO.File.Exists(file)){
i++;
info.FileTile = reader["Title"].ToString();
info.FileUrl = reader["FileURL"].ToString();
collect.Add(info);
}
}


Tstring.CommUtil.Serialize(collect);




#region 序列化和反序列化
///
/// 序列化
///

///
public static void Serialize(SubjectCollect collect) {
string path = "Data.db";
if (File.Exists(path))
File.Delete(path);
FileStream fs = new FileStream(path, FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
try {
formatter.Serialize(fs, collect);
}
catch (SerializationException exp) {
throw exp;
}
finally {
fs.Close();
}
}

///
/// 反序列化
///

///
public static SubjectCollect Deserialize() {
SubjectCollect collect;
string path = "Data.db";
if (!File.Exists(path))
return null;
FileStream fs = new FileStream(path, FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
try {
collect = (SubjectCollect)formatter.Deserialize(fs);
}
catch (SerializationException exp) {
throw exp;
}
finally {
fs.Close();
}
return collect;
}
#endregion
#2
.....*******~~~~~~

帐号 没有注册?

密码 忘记密码?

此功能需要先 登录>>
俗话说你不理财,财不理你,该系统将记录您的每笔交易,
最终生成折线图,所有消费一目了然
点击进入>>

英语频道资料非本站原创作品,资料均来自http://www.unsv.com,
我们的服务仅仅是让您更方便的阅读学习.同时也欢迎您分享你的学习经验.
点击进入>>