Use Serializable attribute to mark a class


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

public class BookRecord {
    public String title;
    public int asin;

    public BookRecord(String title, int asin) {
        this.title = title;
        this.asin = asin;

public class SerializeObject {
    public static void Main() {
        BookRecord book = new BookRecord("title",123456789);
        FileStream stream = new FileStream(@"book.obj",FileMode.Create);

        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(stream, book);


This entry was posted in File Stream. Bookmark the permalink.