我有以下问题:当我启动我的应用程序时,设置从文件加载,因此反序列化,当发生这种情况时,我收到以下错误:
{"End of Stream encountered before parsing was completed."} System.Exception {System.Runtime.Serialization.SerializationException}
序列化代码:
using(FileStream write = new FileStream(SETTINGSPATH,FileMode.Create,FileAccess.Write)
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(write,settings);
}
反序列化方法:
using (FileStream read = new FileStream(SETTINGSPATH,FileMode.Open,FileAccess.Read))
{
BinaryFormatter formatter = new BinaryFormatter();
read.Position = 0;
settings = (Settings)formatter.Deserialize(read); // settings is declared as Settings object
}
设置类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
namespace Serie_Counter.Overkoepelend
{
public delegate void SelectedMoveOptionChanged(AutoMoveOption selectedOption, int checkInterval = 30 );
public delegate void EnableAutoMoveChanged(bool EnableAutoMove);
[Serializable]
public class Settings
{
private string serieListSavePath;
private bool autoStart;
private bool enableRember;
private bool closeWithMainForm;
private int warningDelay;
// moving options
private bool enableAutoMove;
private string rootFolder;
private int checkInterval;
private AutoMoveOption selectedMoveOption;
public event SelectedMoveOptionChanged selectedMoveOptionChanged;
public event EnableAutoMoveChanged enableAutoMoveChanged;
#region Properties
public string SerieListSavePath
{
get
{
return serieListSavePath;
}
set
{
serieListSavePath = value;
}
}
public bool AutoStart
{
get
{
return autoStart;
}
set
{
autoStart = value;
}
}
public bool EnableRember
{
get
{
return enableRember;
}
set
{
enableRember = value;
}
}
public bool CloseWithMainForm
{
get
{
return closeWithMainForm;
}
set
{
closeWithMainForm = value;
}
}
public int WarningDelay
{
get
{
return warningDelay;
}
set
{
warningDelay = value;
}
}
public bool EnableAutoMove
{
get
{
return enableAutoMove;
}
set
{
enableAutoMove = value;
if (enableAutoMove != null) enableAutoMoveChanged(value);
}
}
public string RootFolder
{
get
{
return rootFolder;
}
set
{
rootFolder = value;
}
}
public int CheckInterval
{
get
{
return checkInterval;
}
set
{
checkInterval = value;
}
}
public AutoMoveOption SelectedMoveOption
{
get
{
return selectedMoveOption;
}
set
{
selectedMoveOption = value;
selectedMoveOptionChanged(value, checkInterval);
}
}
#endregion
public Settings(string serieListSavePath)
{
this.serieListSavePath = serieListSavePath;
}
public Settings()
{
this.serieListSavePath = "series.xml";
warningDelay = -1;
}
[OnDeserialized]
private void SetValuesOnDeserialized(StreamingContext context)
{
selectedMoveOptionChanged = null;
enableAutoMoveChanged = null;
}
有人知道为什么会这样吗?
如果您想了解更多信息或代码,请查看http://seriescounter.codeplex.com/
电贺 托马斯
编辑:问题可能是反序列化失败了,因为我将事件序列化为? 我只是通过确保序列化时事件为空来测试它。到目前为止,错误并没有再次发生。
http://seriescounter.codeplex.com/SourceControl/changeset/changes/12646