持久化属性
cs持久化属性,举个例子就是:一个textbox设置一个值后,下次启动程序的时候不变,每次都是上次设置过的值
不使用xml,txt等配置文件
[解决办法]
写个类,用序列化保存,读取时,反序列化
/// <summary>
/// 序列化
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="value">T 对象</param>
/// <param name="filename">文件路径</param>
/// <returns>返回T对象</returns>
public bool Serialize<T>(T value, string filename)
{
try
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bs = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.FileStream stream = new System.IO.FileStream(filename, System.IO.FileMode.Create);
bs.Serialize(stream, value);
stream.Close();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="filename">文件路径</param>
/// <returns></returns>
public T DeSerialize<T>(string filename)
{
T obj;
if (!System.IO.File.Exists(filename))
return default(T);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bs = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.FileStream stream = new System.IO.FileStream(filename, System.IO.FileMode.Open);
obj = (T)bs.Deserialize(stream);
stream.Close();
return obj;
}