空路径名不合法
所以我正在尝试编写一个小行星游戏。它几乎可以工作,所有文件都已到位等等......
当它遇到此代码时就会出现问题。
FileStream myFileStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
string myTempFile = @"F:\Documents\Junior School\Computer Programming (Java 1)\AsteroidsWithSound\AsteroidsWithSound\temp\mysound" + i.ToString() + ".wav";
它给了我一个错误/警告,不确定它到底叫什么,但它说
ArgumentException 未处理。 空路径名不合法。
我在网上阅读过有关导致此问题的代码块的信息,但从未找到解决方案。任何帮助都会很棒。
编辑:文件名在此块中定义。
string filename = this.Player.FileName;
this.Player.Open("");
File.Delete(filename);
this.isReady = true;
So I'm trying to compile an Asteroids game. It's almost working, all the files are in place etc etc...
The issue comes when it hits this code.
FileStream myFileStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
string myTempFile = @"F:\Documents\Junior School\Computer Programming (Java 1)\AsteroidsWithSound\AsteroidsWithSound\temp\mysound" + i.ToString() + ".wav";
It gives me an Error/Warning, not sure exactly what it is called but it says
ArgumentException was unhandled.
Empty path name is not legal.
I've read online about chunks of code like this causing this issue but could never find a resolution. Any help would be awesome.
EDIT: Filename is defined in this chunk.
string filename = this.Player.FileName;
this.Player.Open("");
File.Delete(filename);
this.isReady = true;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这表明
filename
变量引用了一个空字符串。您尚未显示设置
filename
值的代码,但这是需要查看的部分。That suggests that the
filename
variable refers to an empty string.You haven't shown the code that sets the value of
filename
, but that's the bit to look at.首先,尝试看看当您在文件名上放置“监视”并在抛出异常的行处中断时会得到什么。如果它是空的,那么找出它何时设置为空字符串,如果它不空,那么这里出了问题,它可能是某个地方另一个(邪恶的)代码片段的结果。
接下来,我建议您使用 File.readXXXXX 来读取文件,而不是使用新的 FileStream。 File 类可以很好地处理打开-读取-关闭过程。
希望有帮助
First of, try and see what you get when you put a Watch on filename, and break at the exception-throwing line. If it's empty, then find out when was it set to the empty string, if it's not empty, then something's very wrong here and it might be the result of another (evil) code piece somewhere.
Next, I'll suggest you use File.readXXXXX to read the file and not a new FileStream. The File class can handle the open-read-close procedure very nicely.
Hope it helps