简体   繁体   English

System.InvalidCastException:'指定的强制转换无效。

[英]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: 我正在尝试使用下面的代码使用memorystream和filestream和一个结构读取二进制文件:

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: GameSaved结构如下所示:

[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.' System.InvalidCastException:'指定的强制转换无效。

Edit: This is how I save my GameSaved struct: 编辑:这就是我保存GameSaved结构的方法:

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. 您应该使用BinaryFormatter.Serialize方法来序列化对象。 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.InvalidCastException:指定的强制转换无效。 -DynamoDB查询 - System.InvalidCastException: Specified cast is not valid. - DynamoDB Query System.InvalidCastException:“指定的演员表无效。” C# MYSQL - System.InvalidCastException: 'Specified cast is not valid.' C# MYSQL System.InvalidCastException:指定的强制转换无效。错误 - System.InvalidCastException: Specified cast is not valid. Error Xamarin 形式:System.InvalidCastException:“指定的强制转换无效。” - Xamarin forms: System.InvalidCastException: 'Specified cast is not valid.' 在 xamarin 项目 System.InvalidCastException:“指定的演员表无效。” - in xamarin project System.InvalidCastException: 'Specified cast is not valid.' 指定的转换无效-System.InvalidCastException - Specified cast is not valid - System.InvalidCastException System.InvalidCastException:指定的强制转换无效 - System.InvalidCastException: Specified cast is not valid C#System.InvalidCastException:'指定的转换无效。' 将MySQL与Discord.Net一起使用时 - C# System.InvalidCastException: 'Specified cast is not valid.' When using MySQL with Discord.Net Xamarin 表单与 Firebase。 数据检索抛出 System.InvalidCastException: '指定的转换无效。' - Xamarin Forms with Firebase. Data retrival throwing System.InvalidCastException: 'Specified cast is not valid.' “ System.InvalidCastException:指定的转换无效”-DateTime - “System.InvalidCastException: Specified cast is not valid” - DateTime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM