
生成.dat文件,实际上就是运用C#的序列化方法操作而已。
给你一个例子,就是保存C#的对象作为.dat文件dat info是什么格式dat info是什么格式,注意这里保存的是一个Dictionary对象:
public static Dictionary dic_FarmCropsInfo = new Dictionary(); //需要保存的对象
1、读取文件信息
///
/// 获取作物信息
///

public static void GetFarmCropsInfo()
{
try
{
string cropinfo_fn = AppDomain.CurrentDomain.BaseDirectory + "CropsInfo.dat";
using (System.IO.FileStream fs = new System.IO.FileStream(cropinfo_fn, System.IO.FileMode.Open))
{

BinaryFormatter bf = new BinaryFormatter();
dic_FarmCropsInfo = bf.Deserialize(fs) as Dictionary;
}
}
catch { }
}
2、存储文件信息

///
/// 存储作物信息
///
public static void SetFarmCropsInfo()
{
try
{

string cropinfo_fn = AppDomain.CurrentDomain.BaseDirectory + "CropsInfo.dat";
using (System.IO.FileStream fs = new System.IO.FileStream(cropinfo_fn, System.IO.FileMode.OpenOrCreate))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, dic_FarmCropsInfo);
}
}
catch { }
}
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/shumachanpin/article-144085-1.html
函数加油
该出去玩就出去玩
必须抵制美国货