user990635
user990635

Reputation: 4279

Getting a path of a Stream by converting to FileStream

I need to get the path of the original file in the stream.

I read that I need to use the "Name" property of the FileStream to get it's path.
So far so good.

The problem is I have a regular System.IO.Stream and not FileStream, and therefore I don't have the "Name" property.

I read that there is a Stream.CopyTo method: http://msdn.microsoft.com/en-us/library/dd782932.aspx

I planned to convert my Stream to FileStream like that:

System.IO.FileStream fileStream = new System.IO.FileStream(?????);
using (stream)
{
    stream.CopyTo(fileStream);
}  

But FileStream has no parameterless constructor.

How can I solve it?

Upvotes: 0

Views: 5108

Answers (2)

Matthew Mcveigh
Matthew Mcveigh

Reputation: 5685

Assuming the Stream is an instance of a FileStream you can cast the Stream down to a FileStream:

var fileStream = stream as FileStream;

The only way you'll be able to get the path name from the Stream instance is if you are able to cast it down to a class that provides a way of accessing the path.

Upvotes: 2

lordkain
lordkain

Reputation: 3109

getting path of stream

System.IO.FileStream Stream = ....;
FileStream fs = stream as FileStream;
if (fs != null)
{
    // now you can check its path
}
else
{
    // you cant check its path
}

Upvotes: 1

Related Questions