C # create a dat file and read data from the DAT file

Source: Internet
Author: User

Create a dat file and read the data DAT file *. Dat can also be understood from the suffix meaning: data file, data file;

Some of the files can be opened using notepad tools, but they are not necessarily encrypted;

The following uses the C # tool to create a dat file and store it in binary sequence data. In this way, opening it with notepad is garbled and serves as a protection function.

First introduce: using system. IO;

Where to create: Write the following code

Create a BAT file:

Stream S = file. Open ("A. Bat", filemode. Create); // create a. BAT file. If an error occurs before the. BAT file is overwritten, then the file is created

Binaryformatter B = new binaryfitmatter (); // create a serialized object www.kanzhibotv.com

B. serialize (S, "data to be serialized"); // serialize the data to S

S. Close ();

/// Read www.k2tiyu.com

Read the BAT file:

Stream S = file. Open ("A. Bat", filemode. Open); // open the. BAT file

Binaryformatter B = new binaryfitmatter (); // create a serialized object

String SSS = (string) B. deserialize (s); // deserializes s to the original data format;

Public static void writedat (string pfiledat)
{
// Open a binary writer for the file
Filestream FS;
FS = new filestream (pfiledat + "\ bin \ binfile. dat", filemode. openorcreate, fileaccess. readwrite );
Binarywriter BW = new binarywriter (FS );
// Prepare different types of data
Int aint = 34567;
Int bint = 99999;
Int CINT = 11;
Long time = datetime. Today. tobinary ();
Char [] achararray = {'A', 'B', 'C '};
// Write data using multiple reloads of the write Method
Bw. Write (aint );
Bw. Write (bint );
Bw. Write (CINT );
Bw. Write (time );
Bw. Write (achararray );
FS. Close ();
Bw. Close ();
}

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.