Tuesday, September 23, 2014

BinaryReaderWriter_Instr


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace BinaryReaderWriter
{
    public class Car
    {
        private string _make;
        private string _model;
        private int _year;
        private int _mileage;
        private decimal _price;

        public Car(string make, string model, int year, int mileage, decimal price)
        {
            _make = make;
            _model = model;
            _year = year;
            _mileage = mileage;
            _price = price;
        }

        public string Make
        { get { return _make; } }

        public string Model
        { get { return _model; } }

        public int Year
        { get { return _year; } }

        public int Mileage
        { get { return _mileage; } }

        public decimal Price
        { get { return _price; } }
    }
}
=================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO; //Add to read and write

namespace BinaryReaderWriter
{
    public partial class Form1 : Form
    {
        string filepath = "cardata.dat";

        public Form1()
        {
            InitializeComponent();
        }

        private void btnSaveToFile_Click(object sender, EventArgs e)
        {
            ///Need a FileStream object to
            ///open/create file
            ///BinaryWriter to write to a binary file
            ///BinaryReader to read a binary file
            ///
            FileStream fs = null;
            BinaryWriter br = null;
            try
            {
                //1. open file for writing or appending
                fs = new FileStream(filepath,
                                    FileMode.Append,
                                    FileAccess.Write);
                br = new BinaryWriter(fs);
                //2. write object data to the file
                string make = txtMake.Text;
                string model = txtModel.Text;
                int year = int.Parse(txtYear.Text);
                int mileage = int.Parse(txtMileage.Text);
                decimal price = decimal.Parse(txtPrice.Text);

                //save
                br.Write(make);
                br.Write(model);
                br.Write(year);
                br.Write(mileage);
                br.Write(price);

                MessageBox.Show("Data has been saved",
                                "Car data",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }

            catch (FormatException fe)
            {
                MessageBox.Show(fe.Message);
            }
            catch (ArgumentException ae)
            {
                MessageBox.Show(ae.Message);
            }
            catch (IOException ioe)
            {
                MessageBox.Show(ioe.Message);
            }
            finally
            {
                //3. close file
                if (br != null) br.Close();
                if (fs != null) fs.Close();
            }
        }

        private void btnReadCarData_Click(object sender, EventArgs e)
        {
            //read binary file
            //Need to use FileStream to open the file for reading
            //Need to use BinaryReader to read binary data from file
            FileStream fs = null;
            BinaryReader br = null;

            try
            {
                fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
                br = new BinaryReader(fs);

                listView1.Items.Clear();

                while (br.PeekChar() != -1)
                {
                    string make = br.ReadString();
                    string model = br.ReadString();
                    int year = br.ReadInt32();
                    int mileage = br.ReadInt32();
                    decimal price = br.ReadDecimal();

                    //save to a listview
                    //create a listviewitem
                    ListViewItem lvitem = new ListViewItem(make);
                    lvitem.SubItems.Add(model);
                    lvitem.SubItems.Add(year.ToString());
                    lvitem.SubItems.Add(mileage.ToString("n0"));
                    lvitem.SubItems.Add(price.ToString("c"));
                    //add lvitem to the listview
                    listView1.Items.Add(lvitem);
                }
            }
            catch (FormatException fe)
            {
                MessageBox.Show(fe.Message);
            }
            catch (ArgumentException ae)
            {
                MessageBox.Show(ae.Message);
            }
            catch (IOException ioe)
            {
                MessageBox.Show(ioe.Message);
            }

            finally
            {
                if (fs != null) fs.Close();
                if (br != null) br.Close();
            }
        }
    }
}

No comments:

Post a Comment