Markus
Markus

Reputation: 1485

C# Why i get a NotSupported Exception on my file Path

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?

Upvotes: 1

Views: 5530

Answers (3)

Amal Sirisena
Amal Sirisena

Reputation: 1479

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.

Upvotes: 8

bobbymcr
bobbymcr

Reputation: 24177

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.

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1064184

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)

Upvotes: 4

Related Questions