Reputation: 21
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
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