C# 为什么我的文件路径上出现 NotSupported 异常
StreamReader fr = new StreamReader("D:\\test\\" + item);
这就是我想做的。项目是带有文件名的字符串。空字符串就像
"D:\\test\\01-Marriotts Island.mp3"
他尝试生成 StreamReader 之前的那样。 路径有什么问题吗?
StreamReader fr = new StreamReader("D:\\test\\" + item);
This is what i want to do. Item is a String with the filename. The hole string is like that
"D:\\test\\01-Marriotts Island.mp3"
befor he tries to generate the StreamReader.
whats wrong with the path?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
StreamReader 是为读取字符数据而设计的。如果您尝试这样做,则应该使用 BinaryReader读取二进制数据,例如 mp3 文件的内容。
更新:正如马克指出的,您还可以使用 Stream 读取文件,这可能提供比 BinaryReader 更易于使用的界面来操作文件。另外,我赞同他的建议,在构建时使用 Path.Combine您要访问的文件的路径。
StreamReader is designed for reading character data. You should use BinaryReader instead if you are trying to read a binary data, such as the contents of an mp3 file.
Update: As Marc pointed out you could also use a Stream to read the file and this may provide an easier to use interface for manipulating the file than BinaryReader. Also, I second his recommendation to use Path.Combine when building up the path to the file you want to access.
还有其他消息吗?有关信息,组合路径的最简单方法是使用
Path.Combine
:(另请注意
using
以确保其已处理)或更清晰(IMO):(
当然,正如其他地方提到的,
StreamReader
可能不适合 mp3)Is there any more message that goes with it? For info, the easiest way to combine paths is with
Path.Combine
:(note also the
using
to ensure it is disposed)or clearer still (IMO):
(of course, as has been mentioned elsewhere, a
StreamReader
may be inappropriate for mp3)查阅 StreamReader 的 MSDN 文档,我没有看到
NotSupportedException
列为此 API 将抛出的异常。但是,另一个类似的构造函数重载确实列出了它:所以我自己用无效的卷标尝试了一下,确实得到了
NotSupportedException
:所以我猜你的路径有问题。
Consulting the MSDN documentation for StreamReader, I don't see
NotSupportedException
listed as an exception that this API will throw. However, another similar constructor overload does list it:So I tried it myself with an invalid volume label and indeed got
NotSupportedException
:So my guess is there is something wrong with your path.