Olivia N John
Olivia N John

Reputation: 21

Find out file extension from a System.IO.Stream object

I have a method which returns a System.IO.Stream object initialized from a file. I want to assert that the stream object was created for a file with the correct file extension.

Is there any way I can do this?

Thanks

Upvotes: 2

Views: 7126

Answers (1)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262979

Since your Stream object was initialized from a file (hopefully, from a file name), it should be a FileStream. Therefore, you can use its Name property to obtain the name of the underlying file:

FileStream fileStream = yourStream as FileStream;
if (fileStream != null) {
    string extensionWithDot = Path.GetExtension(fileStream.Name);
    // Now test the file extension.
}

Upvotes: 4

Related Questions