惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - 3stones

.NET平台下WEB应用程序部署(安装数据库和自动配置)转 调试自定义操作/安装程序类的方法<转> - 3stones - 博客园 Sql Server中的集合操作Union Intersect Except 如何创建/安装/调试Windows服务&lt;转&gt; 安装 启动 停止 卸载 Windows服务 c# 利用Mircosoft URLRewriter.dll实现页面伪静态 [C#]将数字前面补0,补足设定的长度 加壳工具简介(图) 查壳脱壳工具介绍(图) .net制作的wap网站在手机中的测试 - 3stones - 博客园 vs2008开发wap网站(一) 英文面试自我介绍(一) 丁香园-英语面试精选 英语面试自我介绍范文(二) Remoting的注册与注销 - 3stones - 博客园 创建一个从数据库读入内容的远程对象 c#中的remoting和webservice有什么区别 经典推荐--.Net面试法宝(面试题收集) - 3stones Response.Redirect(),Server.Transfer(),Server.Execute()的区别
用IFormatter实现&gt;存储容器(功能:实现游戏保存和读取)
3stones · 2009-08-05 · via 博客园 - 3stones

用IFormatter实现>存储容器(功能:实现游戏保存和读取)
(一)引言

    用using System.Runtime.Serialization命名空间中的:IFormatter实现存储容器.
    用堆栈的实现原理.  可以存储几乎.Net所有的对象.
   本实现实现用它存储Form窗体中的所有对象,包括Label/TextBox/任意类等状态值.

  保存到文件中,并从文件中读取数据.

(二)代码

     说明:  下面代码实现的是存储象棋游戏棋盘上的所有对象,比如: 用于保存某一时刻棋盘的位置,以及各种控件的状态值等.  读者可以用此方法来保存一些应用程序中的状态值等.

为了便于理解,把整个文件都显示出来了.主要包括两个方法:  保存(序列化)和读取(反序列)

    using System;
using System.IO;
using System.Drawing;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace 智能象棋游戏
{
 /// <summary>
 /// 功能:实现 游戏 保存和读取

 /// 特点:不但能够恢复断点,而且还能够在相应的断点执行退旗等操作
 /// </summary>
 public class Class6
 { 
     public string openFileName="..\\..\\save\\SAVA.bin";   //存储文件名
     public string saveFileName="..\\..\\save\\SAVA.bin";   //存储文件名
 
  public Class6()
  {
   
  }
 
  public void Save(Form2 form2)
  {
      //******序列化Form2中的数组和变量和类*******//
      IFormatter formatter=new BinaryFormatter();   //定义类,主要用此类中的两个方法来实现功能
      Stream stream=new FileStream     (this.saveFileName,FileMode.Create,FileAccess.Write,FileShare.None);  
  
      //序列化Form2 中pictureBoxStatus数组
      formatter.Serialize(stream,form2.pictureBoxsStatus);

   //存储象棋的当前残局,各个棋子的位置等,以及各方的分数,下棋用时等
      formatter.Serialize(stream,form2.battle);
      formatter.Serialize(stream,form2.soldiers);
      formatter.Serialize(stream,form2.tempPoint);
      formatter.Serialize(stream,form2.whetherSelected);
      formatter.Serialize(stream,form2.letDown);
      formatter.Serialize(stream,form2.signRight);
      formatter.Serialize(stream,form2.start);
      formatter.Serialize(stream,form2.wrongString);
      formatter.Serialize(stream,form2.startTime);
      formatter.Serialize(stream,form2.displaystartTime);
      formatter.Serialize(stream,form2.pause);
      formatter.Serialize(stream,form2.whichFangQianZou);
      formatter.Serialize(stream,form2.pauseCount);
      formatter.Serialize(stream,form2.label3_ShanShuo);
      formatter.Serialize(stream,form2.add_Or_Not);
      formatter.Serialize(stream,form2.shanShuoVarialbe);
      formatter.Serialize(stream,form2.whoGoString);

  
      //序列化Form2中类Class2 a中的变量和数组;
      formatter.Serialize(stream,form2.a.GenEatGen); formatter.Serialize (stream,form2.a.have); formatter.Serialize(stream,form2.a.who); formatter.Serialize(stream,form2.a.str); formatter.Serialize(stream,form2.a.allPoint); formatter.Serialize(stream,form2.a.partPoint); formatter.Serialize(stream,form2.a.whichPicture); formatter.Serialize(stream,form2.a.rednum); formatter.Serialize(stream,form2.a.bluenum); formatter.Serialize(stream,form2.a.width); formatter.Serialize(stream,form2.a.height); formatter.Serialize(stream,form2.a.first_X); formatter.Serialize(stream,form2.a.first_Y); formatter.Serialize(stream,form2.a.blueNumTotal); formatter.Serialize(stream,form2.a.redNumTotal);
    
          
   //序列化Form2中类Class5 keyBoard中的变量  
   formatter.Serialize(stream,form2.keyBoard.KeyBoardOperating);
   formatter.Serialize(stream,form2.keyBoard.MouseOperating);  
  
  
   stream.Close();
  }

  public void Read(ref Form2 form2)
  {
     IFormatter formatter=new BinaryFormatter();
     Stream stream=new FileStream(this.openFileName,FileMode.Open,FileAccess.Read,FileShare.Read);
  
     //Form2 a=new Form2();
     Form2 a=form2;
  
     //反序列化Form2 中pictureBoxStatus数组
     a.pictureBoxsStatus=(bool[])formatter.Deserialize(stream);
  
     a.battle=(string)formatter.Deserialize(stream);
     a.soldiers=(string[])formatter.Deserialize(stream);
     a.tempPoint=(Point[])formatter.Deserialize(stream);
     a.whetherSelected=(bool)formatter.Deserialize(stream);
     a.letDown=(bool)formatter.Deserialize(stream);
     a.signRight=(int)formatter.Deserialize(stream);
     a.start=(bool)formatter.Deserialize(stream);
            a.wrongString=(string)formatter.Deserialize(stream);
     a.startTime=(DateTime)formatter.Deserialize(stream);
     a.displaystartTime=(bool)formatter.Deserialize(stream);
     a.pause=(bool)formatter.Deserialize(stream);
     a.whichFangQianZou=(bool)formatter.Deserialize(stream);
     a.pauseCount=(int)formatter.Deserialize(stream);
     a.label3_ShanShuo=(bool)formatter.Deserialize(stream);
     a.add_Or_Not=(bool)formatter.Deserialize(stream);
     a.shanShuoVarialbe=(int)formatter.Deserialize(stream); 
     a.whoGoString=(string)formatter.Deserialize(stream);  
   
     //反序列化Form2中类Class5 keyBoard中的变量
     a.keyBoard.KeyBoardOperating=(bool)formatter.Deserialize(stream);
     a.keyBoard.MouseOperating=(bool)formatter.Deserialize(stream);   
         
     stream.Close();
    } 
  }
}

需要注意的一点时,在往容器里存储和从容器里读取时,要按照椎栈的原理进行存储和读取.

遵守“先进后出,后进先出原则”。

即先放到容器里的对象,在读取时到最后才能读取出来.最后存储的对象,在读取时第一个被读取出来.

FileStream 文件流 操作文件用
binarystream 二进制流 读写图片时用

streamreader 流的读写