Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从二进制文件读取列表_C#_List_Binaryfiles - Fatal编程技术网

C# 从二进制文件读取列表

C# 从二进制文件读取列表,c#,list,binaryfiles,C#,List,Binaryfiles,我遇到的问题是,我可以将文件准备好,但我的读取代码似乎只读取2值列表中的1值。我很困惑这是哪里出了问题。代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;

我遇到的问题是,我可以将文件准备好,但我的读取代码似乎只读取2值列表中的1值。我很困惑这是哪里出了问题。代码如下:

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;

 namespace test
 {

public partial class Form1 : Form
{
    [Serializable]
    public class ore
    {
        public float Cost;

    }



    private List<ore> oreData = new List<ore>();
    private ore b1 = null;
    private ore b2 = null;
    public Form1()
    {
        InitializeComponent();
        b1 = new ore();
        b2 = new ore();
        oreData.Add(b1);
        oreData.Add(b2);

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        // 1st text box input is float
        float tempFloat;


        if (float.TryParse(textBox1.Text, out tempFloat))
        {
            oreData[0].Cost = tempFloat;
        }
        else
            MessageBox.Show("uh oh");



    }


    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        // 2nd text box input is float
        float tempFloat;
        if (float.TryParse(textBox1.Text, out tempFloat))
        {
            oreData[1].Cost = tempFloat;
        }
        else
            MessageBox.Show("uh oh");


    }


    private void button1_Click(object sender, EventArgs e)
    {
        FileStream fs = new FileStream("ore.dat", FileMode.Create);
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(fs, oreData);
        fs.Close();
    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {
        // 3rd text box 
    }

    private void textBox4_TextChanged(object sender, EventArgs e)
    {
        //4th text box
    }


    private void button2_Click(object sender, EventArgs e)
    {
        FileStream fs = new FileStream("ore.dat", FileMode.Open);
        BinaryFormatter bf = new BinaryFormatter();
        oreData = (List<ore>)bf.Deserialize(fs);
        fs.Close();

        if (oreData!=null)
        {
            if (oreData.Count > 0)
                textBox3.Text = oreData[0].Cost.ToString();//update the 3rd text box
            if (oreData.Count > 1)
                textBox4.Text = oreData[1].Cost.ToString();//update the 4th text box

        }
    }
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.IO;
使用System.Runtime.Serialization.Formatters.Binary;
名称空间测试
{
公共部分类Form1:Form
{
[可序列化]
公共级矿石
{
公共浮动成本;
}
私有列表oreData=新列表();
私有矿b1=null;
私有oreb2=null;
公共表格1()
{
初始化组件();
b1=新矿石();
b2=新矿石();
oreData.Add(b1);
oreData.Add(b2);
}
私有void textBox1\u TextChanged(对象发送方,事件参数e)
{
//第一个文本框输入为float
浮动时间浮动;
if(float.TryParse(textBox1.Text,out tempFloat))
{
oreData[0]。成本=tempFloat;
}
其他的
MessageBox.Show(“uh-oh”);
}
私有void textBox2_TextChanged(对象发送方,事件参数e)
{
//第二个文本框输入为浮点
浮动时间浮动;
if(float.TryParse(textBox1.Text,out tempFloat))
{
oreData[1]。成本=临时浮动;
}
其他的
MessageBox.Show(“uh-oh”);
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
FileStream fs=newfilestream(“ore.dat”,FileMode.Create);
BinaryFormatter bf=新的BinaryFormatter();
序列化(fs、oreData);
fs.Close();
}
私有void textBox3\u TextChanged(对象发送方,事件参数e)
{
//第三个文本框
}
私有void textBox4\u TextChanged(对象发送方,事件参数e)
{
//第四个文本框
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
FileStream fs=newfilestream(“ore.dat”,FileMode.Open);
BinaryFormatter bf=新的BinaryFormatter();
oreData=(列表)bf.反序列化(fs);
fs.Close();
如果(oreData!=null)
{
如果(oreData.Count>0)
textBox3.Text=oreData[0]。Cost.ToString();//更新第三个文本框
如果(oreData.Count>1)
textBox4.Text=oreData[1]。Cost.ToString();//更新第四个文本框
}
}
}
}

您需要定义一个列表并将b1、b2项添加到其中,然后每当您序列化或反序列化数据时,将其反序列化到同一个列表中

注意:将文本框和按钮重命名为可读性更好的名称也是一个好主意,例如:代替
textBox2
=>
firstbookepertextbox
textBox2\u TextChanged
=>
firstbookepertextbox\u TextChanged
button1
=>
saveBooksButton
Form1
=>
BooksMainForm

<强> Update:还考虑只使用列表而不是B1和B2,如Boo[0 ]而不是B1和Booe[2]而不是B2,因此如果书籍的数量增加,代码的可维护性将不成问题。请注意,您使用的是一个列表来序列化和反序列化

[Serializable]
public class ore
{
    public float Cost;
}

private List<ore> books = new List<ore>(); // create a list at class level

public Form1()
{
    InitializeComponent();

    books.Add(new ore());
    books.Add(new ore());
}


private void textBox1_TextChanged(object sender, EventArgs e)
{
    // 1st text box input is float
    float tempFloat;
    if (float.TryParse(textBox1.Text, out tempFloat))
    {
        books[0].Cost = tempFloat;
    }
    else
        MessageBox.Show("uh oh");
}


private void textBox2_TextChanged(object sender, EventArgs e)
{
    // 2nd text box input is float
    float tempFloat;
    if (float.TryParse(textBox2.Text, out tempFloat))
    {
        books[1].Cost = tempFloat;
    }
    else
        MessageBox.Show("uh oh");
}

private void button1_Click(object sender, EventArgs e)
{
    FileStream fs = new FileStream("ore.dat", FileMode.Create);
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(fs, books);
    fs.Close();
}

private void button2_Click(object sender, EventArgs e)
{
    FileStream fs = new FileStream("ore.dat", FileMode.Open);
    BinaryFormatter bf = new BinaryFormatter();

    /*use the old list don't create new one 
    and also the new one you are creating has the same
    name as the class level one which may makes conflicts to you.*/
    books = (List<ore>)bf.Deserialize(fs);

    fs.Close();

    if (books!=null)
    {
        if (books.Count > 0)
        {
            //we don't need d1, d2 any more
            //b1 = books[0];
            textBox3.Text = books[0].Cost.ToString();
        }
        if (books.Count > 1)
        {
            //we don't need d1, d2 any more
            //b2 = books[1];
            textBox4.Text = books[1].Cost.ToString();
        }

    }
}
[可序列化]
公共级矿石
{
公共浮动成本;
}
私有列表图书=新列表();//在类级别创建一个列表
公共表格1()
{
初始化组件();
books.Add(newore());
books.Add(newore());
}
私有void textBox1\u TextChanged(对象发送方,事件参数e)
{
//第一个文本框输入为float
浮动时间浮动;
if(float.TryParse(textBox1.Text,out tempFloat))
{
账簿[0]。成本=临时浮动;
}
其他的
MessageBox.Show(“uh-oh”);
}
私有void textBox2_TextChanged(对象发送方,事件参数e)
{
//第二个文本框输入为浮点
浮动时间浮动;
if(float.TryParse(textBox2.Text,out tempFloat))
{
书籍[1]。成本=临时浮动;
}
其他的
MessageBox.Show(“uh-oh”);
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
FileStream fs=newfilestream(“ore.dat”,FileMode.Create);
BinaryFormatter bf=新的BinaryFormatter();
bf.连载(fs、书籍);
fs.Close();
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
FileStream fs=newfilestream(“ore.dat”,FileMode.Open);
BinaryFormatter bf=新的BinaryFormatter();
/*使用旧列表,不要创建新列表
而且你正在创建的新版本也有相同的版本
命名为可能与您产生冲突的类级别*/
图书=(列表)bf.反序列化(fs);
fs.Close();
如果(书籍!=null)
{
如果(books.Count>0)
{
//我们不再需要d1和d2了
//b1=书籍[0];
textBox3.Text=books[0]。Cost.ToString();
}
如果(books.Count>1)
{
//我们不再需要d1和d2了
//b2=书籍[1];
textBox4.Text=books[1]。Cost.ToString();
}
}
}
我发现了你的问题!(哦,我的眼睛疼!)

查看
textbox1\u changed
textbox2\u changed
方法中的
if
语句。他们都是

if (float.TryParse(textBox1.Text, out tempFloat))
但是第二个应该说

if (float.TryParse(textBox2.Text, out tempFloat))

请注意,
textbox1
已更改为
textbox2
。这应该可以解决您的问题。

我是C#新手,当我向这些行传递一个值时,我不确定这些行是否正在创建一个新列表:b1=new ore();b2=新矿石();不,他们只是在制造矿石。如果您想要一个包含这些对象的列表,您需要类似于在
按钮1\u Click()
中所做的操作。两件事-1。您在评论中提到的行不会创建列表。行
List oreData=new List()
创建列表。2.行
List books=new List()没有做任何事情,因为它