日期:2014-05-18 浏览次数:21080 次
#region 序列化和反序列化
/// <summary>
/// 序列化
/// </summary>
/// <param name="collect"></param>
public void Serialize( object obj ) {
string path = Application.StartupPath + "\\Data.db";
if( File.Exists( path ) )
File.Delete( path );
FileStream fs = new FileStream( path, FileMode.Create );
BinaryFormatter formatter = new BinaryFormatter();
try {
formatter.Serialize( fs, obj );
} catch( SerializationException exp ) {
throw exp;
} finally {
fs.Close();
}
}
/*
* SoapFormatter
* XmlSerializer
* BinaryFormatter
*/
/// <summary>
/// 反序列化
/// </summary>
/// <returns></returns>
public object Deserialize( ) {
string path = Application.StartupPath + "\\Data.db";
if( !File.Exists( path ) )
return null;
FileStream fs = new FileStream( path, FileMode.Open );
BinaryFormatter formatter = new BinaryFormatter();
try {
return (object)formatter.Deserialize( fs );
} catch( SerializationException exp ) {
throw exp;
} finally {
fs.Close();
}
}
#endregion
------解决方案--------------------