应对二进制文件C#的每一行估算

发布于 2025-02-12 13:43:51 字数 1222 浏览 1 评论 0原文

我将对象从GUI中序列化为.txt文件。

private void ReserveTable_Click(object sender, EventArgs e)
    {
        
        Reservation CreateReservation = new Reservation(textBoxFirstName.Text, textBoxSecondName.Text, dateTimePickerDate.Value.ToShortDateString(), comboBoxTable.Text, 5, textBoxPhoneNumber.Text);           
        IFormatter formatter = new BinaryFormatter();
        Stream stream = new FileStream(Globals.filepath,FileMode.Append, FileAccess.Write);
        formatter.Serialize(stream, CreateReservation);
        stream.Close();
    }

之后,我想将这些对象列为表格。但是我不知道如何从每行中获取每个对象,我当前只想使.txt文件的第一个对象。我尝试提取所需的数据,并且它适用于.txt文件的第一行。

是否可以循环.txt文件的每一行thorugh并提取每个对象的数据?还是有更好的方法可以做到这一点?我需要获取每个对象的每个属性以将其显示在表中。

提前致谢。

     private void ShowReservation_Click(object sender, EventArgs e)
    {
        
        IFormatter formatter = new BinaryFormatter();
        Stream stream = new FileStream(Globals.filepath, FileMode.Open, FileAccess.Read);
        Reservation ReadReservation = (Reservation)formatter.Deserialize(stream);
        Console.WriteLine(ReadReservation.Firstname);
        Console.WriteLine(ReadReservation.Lastname);
        stream.Close();
    }

I'm serializing objects to a .txt file from a GUI.

private void ReserveTable_Click(object sender, EventArgs e)
    {
        
        Reservation CreateReservation = new Reservation(textBoxFirstName.Text, textBoxSecondName.Text, dateTimePickerDate.Value.ToShortDateString(), comboBoxTable.Text, 5, textBoxPhoneNumber.Text);           
        IFormatter formatter = new BinaryFormatter();
        Stream stream = new FileStream(Globals.filepath,FileMode.Append, FileAccess.Write);
        formatter.Serialize(stream, CreateReservation);
        stream.Close();
    }

After that I want to deserialize those Objects to a table. But I don't know how to get each object from each line, I'm desirializing currently only the first Object of the .txt file. I've tried to extract the data I wanted and it worked for the first line of the .txt file.

Is it possible to loop thorugh each line of the .txt file and extract the data of each Object? Or is there any better way to do this? I need to get each attribute of each object to display it in a table.

Thanks in advance.

     private void ShowReservation_Click(object sender, EventArgs e)
    {
        
        IFormatter formatter = new BinaryFormatter();
        Stream stream = new FileStream(Globals.filepath, FileMode.Open, FileAccess.Read);
        Reservation ReadReservation = (Reservation)formatter.Deserialize(stream);
        Console.WriteLine(ReadReservation.Firstname);
        Console.WriteLine(ReadReservation.Lastname);
        stream.Close();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文