我无法修改序列化数据(c#)



好吧,我的问题是下一个,我必须在 c# 控制台中制作一个游戏,可以将角色的位置保存在二进制存档中,所以当我重新启动游戏时,角色处于它死亡或关闭游戏时的相同位置。现在,它第一次运行时,它使它变得完美并制作了存档,但是第二次我必须序列化时,它给我抛出了一个运行时错误(mscorlib.dll中发生了类型为"System.ArgumentException"的未处理异常(,它标记了我安全"无法修改"。

知道为什么会这样吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Game
{
    [Serializable]
    struct Position
    {
        public int x;
        public int y;
    }
    class Program
    {
        static void Main(string[] args)
        {
            //FileStream data = new FileStream("gameData.txt", FileMode.OpenOrCreate); //abre el codigo si existe. sino, lo crea.
            //StreamWriter textSaver = new StreamWriter(data); //escritor de texto
            //BinaryWriter scoreSaver = new BinaryWriter(data);
            //BinaryFormatter formatter = new BinaryFormatter();
            //FileStream playerPosData = new FileStream("PlayerPosData.txt", FileMode.OpenOrCreate);
            Position where;
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream playerPosData;
            if (!File.Exists("PlayerPosData.txt"))
            {
                playerPosData = File.Create("PlayerPosData.txt");
                where.x = 50;
                where.y = 20;
            }
            else
            {
                playerPosData = File.OpenRead("PlayerPosData.txt");
                where = (Position)formatter.Deserialize(playerPosData);
            }
            Menu theMenu = new Menu();
            if(theMenu.MoveAndChoose() == true)
            {
                Score theScore = new Game.Score();
                Console.BackgroundColor = ConsoleColor.Green;
                Console.ForegroundColor = ConsoleColor.Red;
                Random rand = new Random();
                Player zero = new Player(where.x, where.y);
                Enemy[] badGuys = new Enemy[20];
                for (int i = 0; i < badGuys.Length; i++)
                    badGuys[i] = new Enemy(rand.Next(0, 79), rand.Next(0, 23));
                Bomb[] mines = new Bomb[5];
                for (int i = 0; i < mines.Length; i++)
                    mines[i] = new Bomb(rand.Next(0, 79), rand.Next(0, 23));
                Console.SetCursorPosition(35, 12);
                Console.WriteLine("GAME START!");
                zero.draw();
                Console.ReadKey();
                while (zero.CheckLife() == true)
                {
                    Console.Clear();
                    zero.draw();
                    if (Console.KeyAvailable)
                    {
                        ConsoleKeyInfo controls = Console.ReadKey();
                        zero.Moverse(controls);
                    }
                 for (int i = 0; i < mines.Length; i++)
                    { 
                        mines[i].Draw();
                        if (mines[i].Sense(zero.getX(), zero.getY()))
                            zero.death();
                    }
                    for (int i = 0; i < badGuys.Length; i++)
                    {
                        badGuys[i].draw();
                        badGuys[i].Move();
                        if (badGuys[i].searchAndKill(zero.getX(), zero.getY()))
                            zero.death();
                    }
                    theScore.scoreUp();
                    if (theScore.getScoreNumber() > theScore.getHighScore())
                        theScore.setHighScore(theScore.getScoreNumber());
                    theScore.Draw();
                    System.Threading.Thread.Sleep(50);
                }
                Console.SetCursorPosition(35, 12);
                Console.WriteLine("GAME OVER");
                //textSaver.WriteLine("Acá va a ir el highscore. Testeando"); //Test de puntuacion
                //scoreSaver.Write(theScore.getHighScore());
                //textSaver.Close();
                //data.Close();
                where.x = zero.getX();
                where.y = zero.getY();
                formatter.Serialize(playerPosData, where); // it stops here and says "it can't be modified" the second time
                playerPosData.Close();
                Console.ReadKey();
            }
        }
    }
}

如果文件存在,则 "playerPosData" 将是

playerPosData = File.OpenRead("PlayerPosData.txt");

问题是,您仅以 READ 身份打开流。如果您编写一些文本,则会出现错误,因为您只能读取流。

查看以下代码以获取可能的解决方案:

 Position where;
        BinaryFormatter formatter = new BinaryFormatter();
        if (!File.Exists("PlayerPosData.txt"))
        {
            File.Create("PlayerPosData.txt");
            where.x = 50;
            where.y = 20;
        }
        else
        {
            using (var playerPosData = File.OpenRead("PlayerPosData.txt"))
            {
                where = (Position)formatter.Deserialize(playerPosData);
            }
        }
        // ...
        using (var playerPosData = File.OpenWrite("PlayerPosData.txt"))
        {
            formatter.Serialize(playerPosData, where); // it stops here and says "it can't be modified" the second time
        }
            Console.ReadKey();

使用 可帮助您在使用块完成后关闭流。它自动调用流的释放方法。

最新更新