从二进制文件

时间:2015-08-20 03:18:05

标签: c# serialization

嘿伙计们我正在制作一个程序,它会取一个人的名字和评论,然后将其序列化为二进制文件。它还必须能够反序列化并将数据加载到表单中。我需要它能够通过使用表单上的按钮来上下文件列表(名称和注释)。 在我的代码中,我把它分成了一个构建对象和表单代码的类,并且我不允许将它分开。 (以防万一有人认为这样做) 表格代码:

private void btnAdd_Click(object sender, EventArgs e)
{
    bool blnValid = true;
    if (string.IsNullOrWhiteSpace(txtName.Text))
    {
        MessageBox.Show("Please enter a valid name");
    }
    if (string.IsNullOrWhiteSpace(txtComment.Text))
    {
        MessageBox.Show("Please enter a valid comment");
        blnValid = false;
    }

    if (blnValid)
    {
        //create class instance and assign property values
        //set file name

        DateTime CurrentDate = DateTime.Now;
        string strFileName;

        strFileName = CurrentDate.ToString("dd-MM-yy-hh-mm-ss") + ".bin";

        // serialize object to binary file

        MessageBox.Show(strFileName);

        Enquiry newEnquiry = new Enquiry();
        newEnquiry.Name = txtName.Text;
        newEnquiry.Comment = txtComment.Text;
        newEnquiry.DOB = dteDOB.Value;
        newEnquiry.WriteToFile(strFileName, newEnquiry);
    }
}

private void btnLoad_Click(object sender, EventArgs e)
{

}

private void btnPrevious_Click(object sender, EventArgs e)
{

}

和类代码:

[Serializable]
class Enquiry
{
    //stores the various values into the enquiry class.
    public string Name { get; set; }

    public DateTime DOB { get; set; }

    public string Comment { get; set; }

    //creates the file, if there isn't one, writes the file, and
    //disables sharing while the program is active. It also formats
    //the file into binary 
    public void WriteToFile(string strFileName, Enquiry newEnquiry)
    {
        IFormatter formatter = new BinaryFormatter();
        Stream stream = new FileStream(strFileName, FileMode.Create, FileAccess.Write, FileShare.None);
        formatter.Serialize(stream, newEnquiry);
        stream.Close();

    }
    // this loads the files and translates them to plain text

    public void ReadFromFile(string strFileName, Enquiry newEnquiry)
    {
        Stream stream = File.OpenRead(strFileName);
        BinaryFormatter formatter = new BinaryFormatter();
        newEnquiry = (Enquiry)formatter.Deserialize(stream);
        stream.Close();     
    }

1 个答案:

答案 0 :(得分:0)

我不知道您在问什么,但如果您需要序列化和反序列化的方法,请使用以下方法:

 public static void BinarySerializeObject(string path, object obj)
    {
      using (StreamWriter streamWriter = new StreamWriter(path))
      {
        BinaryFormatter binaryFormatter = new BinaryFormatter();
        try
        {
          binaryFormatter.Serialize(streamWriter.BaseStream, obj);
        }
        catch (SerializationException ex)
        {
          throw new SerializationException(((object) ex).ToString() + "\n" + ex.Source);
        }
      }
    }

    public static object BinaryDeserializeObject(string path)
    {
      using (StreamReader streamReader = new StreamReader(path))
      {
        BinaryFormatter binaryFormatter = new BinaryFormatter();
        object obj;
        try
        {
          obj = binaryFormatter.Deserialize(streamReader.BaseStream);
        }
        catch (SerializationException ex)
        {
          throw new SerializationException(((object) ex).ToString() + "\n" + ex.Source);
        }
        return obj;
      }
    }