如何恢复在C#中备份的数据?

时间:2013-04-17 02:04:19

标签: c# backup

如何恢复在C#中备份的数据?我试图弄清楚如何恢复已保存的数据。我想我想出了如何保存数据,但是当我的应用程序运行时,我无法恢复数据,我真的需要帮助吗?

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;
using System.Runtime.Serialization.Formatters.Binary;

namespace Lab09_E00988190
{
    public partial class Form1 : Form
    {
        List<Student> listOfStudents;
        int intCurrentStudent = 0;

        string fileName;
        StreamWriter outputFile;
        StreamWriter inputFile;
        BinaryFormatter studentFormatter = new BinaryFormatter();

        public Form1()
        {
            InitializeComponent();
            listOfStudents = new List<Student>();
            txtGPA.Enabled = false;
            txtID.Enabled = false;
            txtName.Enabled = false;
            btnPrevious.Enabled = false;
            btnNext.Enabled = false;
            btnRegisterStudent.Enabled = false;
        }

        private void btnNewStudent_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtGPA.Text = "";
            txtID.Text = "";

            txtName.Enabled = true;
            txtGPA.Enabled = true;
            txtID.Enabled = true;
            txtName.Focus();

            btnRegisterStudent.Enabled = true;
            btnNewStudent.Enabled = true;
        }

       private void btnNext_Click(object sender, EventArgs e)
       {
            intCurrentStudent++;
            displayCurrent();
            if (intCurrentStudent == listOfStudents.Count - 1)
            {
               btnNext.Enabled = false;
            }
            btnPrevious.Enabled = true;
       }

        private void displayCurrent()
        {
            txtGPA.Text = listOfStudents[intCurrentStudent].GPA.ToString();
            txtName.Text = listOfStudents[intCurrentStudent].Name;
            txtID.Text = listOfStudents[intCurrentStudent].ID.ToString();
            lblNumberofStudents.Text =     listOfStudents[intCurrentStudent].Students.ToString();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            intCurrentStudent--;
            displayCurrent();
            if (intCurrentStudent == 0)
            {
                btnPrevious.Enabled = false;
            }
            btnNext.Enabled = true;
        }

        private void btnRegisterStudent_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text != "")
                {
                    if (Decimal.Parse(txtGPA.Text) >= 0 && Decimal.Parse(txtGPA.Text) <= 4)
                    {
                        listOfStudents.Add(new Student(txtName.Text, txtID.Text, Int32.Parse(lblNumberofStudents.Text), Decimal.Parse(txtGPA.Text)));
                        txtGPA.Enabled = false;
                        txtID.Enabled = false;
                        txtName.Enabled = false;

                        if (listOfStudents.Count == 0)
                        {
                            btnPrevious.Enabled = false;
                            btnNext.Enabled = false;
                        }

                        if (listOfStudents.Count == 1)
                        {
                            btnPrevious.Enabled = false;
                            btnNext.Enabled = false;
                        }

                        if (listOfStudents.Count > 1)
                        {
                            btnPrevious.Enabled = true;
                            btnNext.Enabled = true;
                        }

                        if (listOfStudents.Count >= 5)
                        {
                            btnRegisterStudent.Enabled = false;
                            btnNewStudent.Enabled = false;
                        }
                }
                else
                {
                    MessageBox.Show("GPA can't be less than 0 or greater than 4");
                }
            }
            else
            {
                MessageBox.Show("GPA must be in numbers!");
            }
        }
        catch
        {
            MessageBox.Show("Make sure you have all the right information entered there");
        }
        displaySummary();
    }

    private void displaySummary()
    {
        int numberOfStudents = 0;


        foreach (Student aStudent in listOfStudents)
        {
            numberOfStudents += aStudent.Students;
        }
        lblNumberofStudents.Text = listOfStudents.Count.ToString();
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void openFileDialogToolStripMenuItem_Click(object sender, EventArgs e)
    {
        DialogResult result = openFileDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            fileName = openFileDialog1.FileName;
            if (!File.Exists(fileName))
            {
                fileName += ".txt";
            }
        }
        {
            try
            {
                using (Stream inputFile = File.OpenRead(fileName))
                {
                    listOfStudents = (List<Student>)studentFormatter.Deserialize(inputFile);
                }
                if (listOfStudents.Count == 0)
                {
                    btnPrevious.Enabled = false;
                    btnNext.Enabled = false;
                }

                if (listOfStudents.Count == 1)
                {
                    btnPrevious.Enabled = false;
                    btnNext.Enabled = false;
                }

                if (listOfStudents.Count > 1)
                {
                    btnPrevious.Enabled = true;
                    btnNext.Enabled = true;
                }

                if (listOfStudents.Count >= 5)
                {
                    btnRegisterStudent.Enabled = false;
                    btnNewStudent.Enabled = false;
                }
                displaySummary();
                displayCurrent();
                MessageBox.Show("File backup complete!");
            }
            catch (ArgumentNullException)
            {
                MessageBox.Show("You need to select file first!");
            }
            catch (IOException exception)
            {
                MessageBox.Show(exception.ToString(), "An IO exception has occured!");
            }
        }   
    }

    private void saveFileDialogToolStripMenuItem_Click(object sender, EventArgs e)
    {
        DialogResult result = saveFileDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            fileName = saveFileDialog1.FileName;
            if (!File.Exists(fileName))
            {
                fileName += ".txt";
            }
        }
        try
        {
            using (Stream outputFile = File.Create(fileName))
            {
                studentFormatter.Serialize(outputFile, listOfStudents);
            }
            MessageBox.Show("File restoration complete!");
        }
        catch (IOException exception)
        {
            MessageBox.Show(exception.ToString(), "An IO exception has occured!");
        }
    }
}

}

0 个答案:

没有答案