简体   繁体   中英

System.InvalidCastException: 'Specified cast is not valid.'

I'm trying to read a binary file, using memorystream and filestream and a struct, with the code below:

BinaryFormatter formatter = new BinaryFormatter();
MemoryStream mStream = new MemoryStream();
byte[] buffer = null;
long numBytes = new FileInfo(filename1).Length;
FileStream fs = new FileStream(filename1, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
buffer = br.ReadBytes((int)numBytes);
mStream.Write(buffer, 0, buffer.Length);
mStream.Position = 0;
GameSaved newdata = (GameSaved)formatter.Deserialize(mStream);
mStream.Close();
fs.Close();
fs.Dispose();
mStream.Dispose();

The GameSaved struct looks like this:

[Serializable]
struct GameSaved
{
    public int Num_Of_Saved_Game;
    public string[] Name_Of_Saved_Game;
}

But the code throws an error

System.InvalidCastException: 'Specified cast is not valid.'

Edit: This is how I save my GameSaved struct:

buffer = null;
formatter = new BinaryFormatter();
mStream = new MemoryStream();
formatter.Serialize(mStream, newdata);
buffer = mStream.ToArray();
mStream.Close();
filename = "name.sav";
curFile = @"c:\C#\Try_To_Save_MS\Try_To_Save_MS\bin\Debug\name.sav";

if (File.Exists(curFile))
    File.Delete(curFile);

fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
fs.Write(buffer, 0, (int)buffer.Length);
fs.Dispose();
mStream.Dispose();

Could anyone please show me the way to solve the problems?

Best Regards

You should use the BinaryFormatter.Serialize method to serialize your object. In your code you write a buffer but probably this don't do the same thing

A sample of serialization of your data could written in this way

GameSaved reloaded = new GameSaved();
void Main()
{
    GameSaved game = new GameSaved();
    game.Num_Of_Saved_Game = 2;
    game.Name_Of_Saved_Game = new string[] {"game1", "game2"};

    Serialize(@"e:\temp\serialize.bin", game);
    Deserialize(@"e:\temp\serialize.bin");

    Console.WriteLine("Games:" + reloaded.Num_Of_Saved_Game);
    foreach(string s in reloaded.Name_Of_Saved_Game)
        Console.WriteLine(s);
}

void Deserialize(string filename1)
{
    BinaryFormatter formatter = new BinaryFormatter();
    using(FileStream fs = new FileStream(filename1, FileMode.Open, FileAccess.Read))
        reloaded = (GameSaved)formatter.Deserialize(fs);
}
void Serialize(string filename1, GameSaved game)
{
    BinaryFormatter formatter = new BinaryFormatter();
    using (FileStream fs = new FileStream(filename1, FileMode.OpenOrCreate, FileAccess.Write))
        formatter.Serialize(fs, game);

}
[Serializable]
struct GameSaved
{
    public int Num_Of_Saved_Game;
    public string[] Name_Of_Saved_Game;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM