我正在尝试使用BinaryFormatter来序列化我正在开发的程序的状态,但是当我尝试反序列化生成的文件时,我收到以下错误.我的代码检查以确保文件名有效(已经在这里看到很多类似的问题后添加了),所以我不明白这个错误是如何发生的:
System.ArgumentException
Message =空路径名称不合法.
源= mscorlib程序
堆栈跟踪:
在System.IO.FileStream.Init(字符串路径,的FileMode模式,FileAccess的访问,权限的Int32,布尔useRights,文件共享份额,缓冲区大小的Int32,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,字符串MSGPATH,布尔bFromProxy,布尔useLongPath)
在System.IO.FileStream..ctor(字符串路径,的FileMode模式,FileAccess的访问,文件共享份额)
在System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(URI的URI,流流,BitmapCacheOption cacheOption,的Guid&CLSID,布尔&isOriginalWritable,
System.Windows.Media上的System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri,Uri uri,Stream stream,BitmapCreateOptions createOptions,BitmapCacheOption cacheOption,RequestCachePolicy uriCachePolicy,Boolean insertInDecoderCache)
中的Stream&uriStream,UnmanagedMemoryStream&unmanagedMemoryStream,SafeFileHandle&safeFilehandle . 位于St的
System.Windows.Media.Imaging.BitmapImage.EndInit()的Imaging.BitmapImage.FinalizeCreation()
C:\ Projects\Dalek\StarShips\Ship.cs中的arShips.Ship..ctor(SerializationInfo info,StreamingContext ctxt):第387行
这是我的Save方法,它最初创建文件:
Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
sfd.FileName = "NewGame";
sfd.DefaultExt = ".sav";
sfd.Filter = "Save Games (.sav)|*.sav";
Nullable<bool> result = sfd.ShowDialog();
if (result == true)
{
string fileName = sfd.FileName;
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
GameTest t = new GameTest();
t.Players = GameState.Players;
formatter.Serialize(stream, …Run Code Online (Sandbox Code Playgroud)