gpt4 book ai didi

c# - 为什么这会产生 "Empty Path Name Is Not Legal"异常?

转载 作者:太空宇宙 更新时间:2023-11-03 20:07:33 25 4
gpt4 key购买 nike

我正在尝试使用 BinaryFormatter 来序列化我正在开发的程序的状态,但是当我尝试反序列化生成的文件时出现以下错误。我的代码检查以确保文件名有效(在看到很多类似问题后已经添加),所以我不明白这个错误是如何发生的:

System.ArgumentException
Message=Empty path name is not legal.
Source=mscorlib
StackTrace:
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle)
at System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache)
at System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()
at System.Windows.Media.Imaging.BitmapImage.EndInit()
at StarShips.Ship..ctor(SerializationInfo info, StreamingContext ctxt) in C:\Projects\Dalek\StarShips\Ship.cs:line 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, t);

MessageBox.Show(string.Format("Save Complete! Size: {0}",stream.Length));
stream.Close();
}

下面是抛出异常的代码:

Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.FileName = "NewGame";
ofd.DefaultExt = ".sav";
ofd.Filter = "Save Games (.sav)|*.sav";
Nullable<bool> result = ofd.ShowDialog();
if (result == true)
{
string fileName = ofd.FileName;
System.Diagnostics.Debug.WriteLine(fileName);
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
stream.Position = 0;
MessageBox.Show(string.Format("size: {0}, Filename: {1}", stream.Length,((FileStream)stream).Name));
GameTest t = (GameTest)formatter.Deserialize(stream); // Error occurs here
stream.Close();
}

我已经尝试在我的文件系统中几乎所有地方保存/加载/从 C 驱动器的根目录甚至在程序的执行文件夹中保存/加载。
我已经尝试进入反序列化,它遍历了大量的对象图,但突然回到我上面的反序列化调用并抛出异常。
反序列化之前的消息框显示流的文件名有效,那么为什么我会收到此消息?

编辑:如果我在调试时继续排除错误,我会收到另一个错误:“在解析完成之前遇到流结束。”我仔细检查了我的所有类,所有 GetObjectData 方法都添加了与序列化构造函数相同的值,所以我不知道它在流之外的什么地方运行。

最佳答案

at StarShips.Ship..ctor(SerializationInfo info, StreamingContext ctxt)

异常的调用堆栈表明您正在寻找这个问题的完全错误的角落。您假设错误的是包含序列化数据的文件路径,堆栈跟踪讲述了一个截然不同的故事。

以防混淆,.ctor 是类构造函数的内部 CLR 名称。所以它实际上是你的 Ship 类构造函数炸弹。它尝试使用 BitmapImage(Uri) 构造函数创建 BitmapImage 对象。有问题的是那个构造函数的参数。可能是因为您忘记序列化位图的文件名,或者没有正确处理此字符串为 null 或空的情况。

在 Ship 构造函数上放置一个断点以单步执行代码。或者使用 Debug + Exceptions,勾选 CLR 异常的 Thrown 复选框,以便调试器在抛出异常时停止。或者删除代码中吞没异常的 try/catch,它会妨碍诊断问题。

关于c# - 为什么这会产生 "Empty Path Name Is Not Legal"异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22082489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com