Ejemplo n.º 1
0
        private void button2_Click(object sender, EventArgs e)
        {
            Stream myStream;
            // TAuto auto = new TAuto();
            List <TAuto>    cars            = new List <TAuto>();
            BinaryFormatter bformatter      = new BinaryFormatter();
            SaveFileDialog  saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter           = "dat files (*.dat)|*.dat";
            saveFileDialog1.FilterIndex      = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    try {
                        foreach (var i in openFile.Split('\n'))
                        {
                            int   j    = 0;
                            TAuto auto = new TAuto();
                            auto.Auto        = i.Trim().Split()[j];
                            auto.Cost        = i.Trim().Split()[++j];
                            auto.Rate        = i.Trim().Split()[++j];
                            auto.Realibility = i.Trim().Split()[++j];
                            auto.Comfort     = i.Trim().Split()[++j];

                            cars.Add(auto);
                        }

                        bformatter.Serialize(myStream, cars);
                    } catch (Exception ex) {
                        MessageBox.Show(ex.Message);
                    } finally {
                        myStream.Close();
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void сохранитьВФайлToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stream myStream;
            // TAuto auto = new TAuto();
            List <TAuto>    cars       = new List <TAuto>();
            BinaryFormatter bformatter = new BinaryFormatter();

            saveFileDialog1.Filter           = "dat files (*.dat)|*.dat|";
            saveFileDialog1.FilterIndex      = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    try {
                        for (int i = 0; i < dataGridView1.RowCount; i++)
                        {
                            int   j    = 0;
                            TAuto auto = new TAuto();
                            auto.Auto        = dataGridView1.Rows[i].Cells[j].Value.ToString() ?? " ";
                            auto.Cost        = dataGridView1.Rows[i].Cells[++j].Value.ToString() ?? " ";
                            auto.Rate        = dataGridView1.Rows[i].Cells[++j].Value.ToString() ?? " ";
                            auto.Realibility = dataGridView1.Rows[i].Cells[++j].Value.ToString() ?? " ";
                            auto.Comfort     = dataGridView1.Rows[i].Cells[++j].Value.ToString() ?? " ";
                            cars.Add(auto);
                        }

                        bformatter.Serialize(myStream, cars);
                    } catch (Exception ex) {
                        MessageBox.Show(ex.Message);
                    } finally {
                        myStream.Close();
                    }
                }
            }
        }