Implement> storage container with iformatter (function: Save and read Games)

Source: Internet
Author: User

Implement> storage container with iformatter (function: Save and read Games)
(1) Introduction

Use the iformatter in the using system. runtime. serialization namespace to implement the storage container.
With the implementation principle of stack, almost all objects in. Net can be stored.
This implementation uses it to store all objects in form forms, including status values such as label, Textbox, and any class.

Save to the file and read data from the file.

(2)Code

Note: The following code stores all objects on a chess game board, such as the position of the Board at a certain time point and the Status values of various controls. you can use this method to save some applications.ProgramStatus value in.

For ease of understanding, the entire file is displayed. There are two main methods: Saving (serialization) and reading (reverse sequence)

Using system;
Using system. IO;
Using system. drawing;
Using system. runtime. serialization;
Using system. runtime. serialization. formatters. Binary;

Namespace chess game
{
/// <Summary>
/// Function: stores and reads games

// features: not only can breakpoint be restored, you can also perform the flag return and other operations at the corresponding breakpoint
///
public class class6
{< br> Public String openfilename =" .. \\.. \ save \ Sava. bin "; // storage file name
Public String savefilename = ".. \\.. \ save \ Sava. bin "; // storage file name
Public class6 ()
{< br>
}< br>
Public void save (form2 form2)
{
// ****** serialize arrays, variables, and classes in form2 ******** //
iformatter formatter = new binaryformatter (); // define the class, mainly using two methods in this class to implement the function
stream = new filestream (this. savefilename, filemode. create, fileaccess. write, fileshare. none);
// serialize the pictureboxstatus array in form2
formatter. serialize (stream, form2.pictureboxsstatus);

// Stores the current mess of chess, the position of each chess piece, scores of all parties, and the time of playing chess.
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 );


// Serialize the variables and arrays in class class2 A of form2;
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 );


// Serialize the variables in the form2 class 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;

// Deserialize the pictureboxstatus array in form2
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 );

// Deserialize the variables in the form2 class class5 keyboard
A. keyboard. keyboardoperating = (bool) formatter. deserialize (Stream );
A. keyboard. mouseoperating = (bool) formatter. deserialize (Stream );

Stream. Close ();
}
}
}

Note that storage and read from and from containers are based on the principles of the spine stack.

Comply with the "first-in-first-out, first-in-first-out" principle ".

That is, the objects first placed in the container can be read at the end of the read. The last stored object is first read at the read.

Filestream file streamingCompositionComponent
Binarystream binary stream used to read and write Images

Streamreader stream read/write

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.