读取.dat数据文件

public static Dictionary<string, serclass> dic_FarmCropsInfo = new Dictionary<string, serclass>();  //需要保存的对象

/// <summary>
        /// 获取
        /// </summary>
        public static void GetFarmCropsInfo()
        {
            try
            {
                string cropinfo_fn = AppDomain.CurrentDomain.BaseDirectory + "object.dat";
                if (!File.Exists(cropinfo_fn))
                {
                    File.Create(cropinfo_fn);
                }
                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<string, PayInfo>;
                }
            }
            catch
            {
            }
        }

/// <summary>
        /// 存储
        /// </summary>
        public static void SetFarmCropsInfo()
        {

try            

{               

  string cropinfo_fn = AppDomain.CurrentDomain.BaseDirectory + "object.dat";              

   if (!File.Exists(cropinfo_fn))               

  {                  

   File.Create(cropinfo_fn);              

   }                

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             {             }

}

 

public class serclass

{

...

}

 

转载于:https://www.cnblogs.com/Charles-xu/p/4913546.html

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页