SerializationException:'尝试反序列化空流。我要反序列化的文件包含内容。怎么了?

时间:2019-02-23 03:15:03

标签: c# serialization

摘要


已简要介绍了创建具有持久性的软件解决方案。但是,我发现我的序列化错误没有道理,因为我要反序列化的对象具有内容并且不为空。我花了大约3个小时来解决不同的版本,以及来自互联网的信息,但都没有成功。很抱歉有大量代码,但是我找不到它出故障的地方。

资源


BinaryFormatter & CryptoStream problem when deserializing

Attempting to deserialize an empty stream?

Runtime error Attempting to deserialize an empty stream

Microsoft Documentation - ISerializable

SerializableAttribute

代码


反序列化

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split('\\')[1];
            string folderName = "c:\\Users\\"+ userName + "\\Documents";
            string pathString = System.IO.Path.Combine(folderName, "Clients");
            System.IO.Directory.CreateDirectory(pathString);
            string[] FileNames = Directory.GetFiles("C:\\Users\\" + userName + "\\Documents\\Clients\\", "*.clnt")
                .Select(System.IO.Path.GetFileName)
                .ToArray();
            if (FileNames.Length > 0)
            {
                for (int Count = 0; Count <= FileNames.Length; ++Count)
                {

                    FileStream s = new FileStream(FileNames[Count] + ".clnt", FileMode.Open, FileAccess.Read);
                    IFormatter formatter = new BinaryFormatter();
                    ClientDataClass Client = (ClientDataClass)formatter.Deserialize(s);
                    s.Close();
                    ClientComboChoice.Items.Add(Client);
                }
            }

        }

序列化

protected ClientDataClass(SerializationInfo info, StreamingContext context )
{
    StClientBrief = info.GetString(StClientBrief);
    StClientName = info.GetString(StClientName);
    StClientEmail = info.GetString(StClientEmail);
    StartDate = info.GetDateTime(StartDate.ToString());
    DueDate = info.GetDateTime(DueDate.ToString());
    Price = info.GetDouble(Price.ToString());
    SFW = info.GetBoolean(SFW.ToString());
    Planning = info.GetBoolean(Planning.ToString());
    PlanName = info.GetString(PlanName);
}

//[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)] Learnt this is obsolete 25/02/2019
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
    info.AddValue("ClientName", StClientName);
    info.AddValue("ClientBrief", StClientBrief);
    info.AddValue("ClientEmail", StClientEmail);
    info.AddValue("StartDate", StartDate);
    info.AddValue("DueDate", DueDate);
    info.AddValue("Price", Price);
    info.AddValue("SFW", SFW);
    info.AddValue("Planning", Planning);
    info.AddValue("Plan Name", PlanName);

}

}

证明文件包含内容


Proof it has contents

Proof it exists

尝试


我尝试更改扩展名(与文件匹配)。我试着删除扩展名。我尝试更改数据类的序列化部分。

问题


怎么了?将来如何避免呢?我该如何解决?

1 个答案:

答案 0 :(得分:1)

由于我无法在评论中添加它:
看来您正在尝试做太多事情。 首先,请分解步骤,并确保在尝试读取文件之前拥有所需的一切。

我的猜测是您要将文件名传递给它,但是您确实需要将整个路径传递给文件。

尝试一下。

string[] FileNames = Directory.GetFiles("C:\\Users\\" + userName + "\\Documents\\Clients\\", "*.clnt");
        foreach(string file in FileNames)
{
   FileStream s = new FileStream(file, FileMode.Open, FileAccess.Read);
   IFormatter formatter = new BinaryFormatter();
   ClientDataClass Client = (ClientDataClass)formatter.Deserialize(s);
   s.Close();
   ClientComboChoice.Items.Add(Client);
}

您可以通过执行以下操作检查文件名

foreach(string file in FileNames)
{
   Console.WriteLine(file);
   //FileStream s = new FileStream(file, FileMode.Open, FileAccess.Read);
   //IFormatter formatter = new BinaryFormatter();
   //ClientDataClass Client = (ClientDataClass)formatter.Deserialize(s);
   //s.Close();
   //ClientComboChoice.Items.Add(Client);
}

如果您可以打开文件,那么我认为您很好,不确定序列化/反序列化操作,但看起来还可以。