C# 为什么我的文件路径上出现 NotSupported 异常

发布于 2024-08-05 10:47:29 字数 223 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猥︴琐丶欲为 2024-08-12 10:47:29

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.

吹梦到西洲 2024-08-12 10:47:29

还有其他消息吗?有关信息,组合路径的最简单方法是使用 Path.Combine:(

using(StreamReader fr = new StreamReader(Path.Combine(@"D:\Test", item))) {
   // ...
}

另请注意 using 以确保其已处理)

或更清晰(IMO):(

using(StreamReader fr = File.OpenText(Path.Combine(@"D:\Test", item))) {
    // ...
}

当然,正如其他地方提到的,StreamReader 可能不适合 mp3)

Is there any more message that goes with it? For info, the easiest way to combine paths is with Path.Combine:

using(StreamReader fr = new StreamReader(Path.Combine(@"D:\Test", item))) {
   // ...
}

(note also the using to ensure it is disposed)

or clearer still (IMO):

using(StreamReader fr = File.OpenText(Path.Combine(@"D:\Test", item))) {
    // ...
}

(of course, as has been mentioned elsewhere, a StreamReader may be inappropriate for mp3)

初见你 2024-08-12 10:47:29

查阅 StreamReader 的 MSDN 文档,我没有看到 NotSupportedException 列为此 API 将抛出的异常。但是,另一个类似的构造函数重载确实列出了它:

NotSupportedException:路径包含
不正确或无效的语法
文件名、目录名或卷
标签。

所以我自己用无效的卷标尝试了一下,确实得到了 NotSupportedException

StreamReader reader = new StreamReader("DD:\\file.txt");

// throws...
//
// Unhandled Exception: System.NotSupportedException: The given path's format is not supported.

所以我猜你的路径有问题。

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:

NotSupportedException: path includes
an incorrect or invalid syntax for
file name, directory name, or volume
label.

So I tried it myself with an invalid volume label and indeed got NotSupportedException:

StreamReader reader = new StreamReader("DD:\\file.txt");

// throws...
//
// Unhandled Exception: System.NotSupportedException: The given path's format is not supported.

So my guess is there is something wrong with your path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文