C# 将数组写入并读取到二进制文件

C# 将数组写入并读取到二进制文件,c#,arrays,binaryfiles,C#,Arrays,Binaryfiles,我有一个由1个字符串值和2个int值组成的数组,我想将其写入一个二进制文件 它由名称、索引和分数组成 我已经附加了下面的数组代码,我如何才能将其写入文件 Player[] playerArr = new Player[10]; int index = 0; index = index + 1; // when a new player is added the index is increased

我有一个由1个字符串值和2个int值组成的数组,我想将其写入一个二进制文件

它由名称、索引和分数组成

我已经附加了下面的数组代码,我如何才能将其写入文件

Player[] playerArr = new Player[10];
        int index = 0;
        index = index + 1;                                  // when a new player is added the index is increased by one
        Player p = new Player(txtName3.Text, index, Convert.ToInt16(txtScore.Text));    // set the values of the object p
        p.refName = txtName3.Text;                          // set refName to be the string value that is entered in txtName
        p.refTotalScore = Convert.ToInt16(txtScore.Text);
        playerArr[index] = p;                               // set the p object to be equal to a position inside the array
我还想按照分数的降序对要输出的数组的每个实例化进行排序。这怎么可能呢

到目前为止,我掌握的文件处理代码是:

    private static void WriteToFile(Player[] playerArr, int size)
    {
        Stream sw;
        BinaryFormatter bf = new BinaryFormatter();

        try
        {
            sw = File.Open("Players.bin", FileMode.Create);
            bf.Serialize(sw, playerArr[0]);
            sw.Close();
            sw = File.Open("Players.bin", FileMode.Append);
            for (int x = 1; x < size; x++)
            {
                bf.Serialize(sw, playerArr[x]);
            }
            sw.Close();
        }
        catch (IOException e)
        {
            MessageBox.Show("" + e.Message);
        }
    }

    private int ReadFromFile(Player[] playerArr)
    {
        int size = 0;
        Stream sr;
        try
        {
            sr = File.OpenRead("Players.bin");
            BinaryFormatter bf = new BinaryFormatter();
            try
            {
                while (sr.Position < sr.Length)
                {
                    playerArr[size] = (Player)bf.Deserialize(sr);
                    size++;
                }
                sr.Close();
            }
            catch (SerializationException e)
            {
                sr.Close();
                return size;
            }
            return size;
        }
        catch (IOException e)
        {
            MessageBox.Show("\n\n\tFile not found" + e.Message);
        }
        finally
        {
            lstLeaderboard2.Items.Add("");
        }
        return size;
    }
私有静态void WriteToFile(播放器[]播放器,整数大小)
{
西南流;
BinaryFormatter bf=新的BinaryFormatter();
尝试
{
sw=File.Open(“Players.bin”,FileMode.Create);
bf.序列化(sw,playerArr[0]);
sw.Close();
sw=File.Open(“Players.bin”,FileMode.Append);
用于(int x=1;x
对于第一部分,您需要将类标记为可序列化,如下所示:

    [Serializable]
    public class Player
可以将
附加到新文件中,这样您就可以将代码更改为:

    sw = File.Open(@"C:\Players.bin", FileMode.Append);
    for (int x = 0; x < size; x++)
    {
        bf.Serialize(sw, playerArr[x]);
    }
    sw.Close(); 
如果需要阵列作为输出,请执行以下操作:

var sortedArray = playerArr.OrderBy(p => p.Score).ToArray();
(此处,
Score
Player
类中要排序的属性的名称。)


如果你想要更多的帮助,你需要更具体的问题

你正在寻找序列化我已经添加了我到目前为止的问题,你提供的代码有什么问题?我不知道,但我不能将数组写入文件你得到一个空文件吗?异常?我在sw=File.Open(@“C:\Players.bin”,FileMode.Append)处遇到错误;错误消息是什么?也许您没有访问
C:
根文件夹的权限?您可以删除文件路径,只需使用
file.Open(“Players.bin”,…)
,这是您的原始位置。我不再有此错误,但我的方法中有一个单独的错误:WriteToFile(playerr,I);作为“当前上下文中不存在的名称”。这不允许程序运行,这与writeToFile方法中的int size有关。我建议您从
writeToFile
方法中删除'size'参数,以便调用
writeToFile(playerr)
,并在
writeToFile
方法的开头添加以下行:
int size=playerr.Count();我在bf.Serialize(sw,playerArr[x])处收到一个错误;-未处理此异常
var sortedArray = playerArr.OrderBy(p => p.Score).ToArray();