Getting the underlying filename of a System.IO.StreamWriter

T

Tom Jones

I am handed a reference to a StreamWriter object. I need to obtain the name
of the underlying file name that this writer is associated with. I can't
find a method to do so - am I missing something?

Thanks,
Yom
 
N

Niki Estner

The BaseStream property of the StreamWriter will give you a reference to the
stream that is used. If you know for sure this stream is a FileStream
instance, you can cast it to that type, and use the Name property the get
the file's name. (according to the docs this is the "name passed to the
constructor" - might be a relative path!)

Niki
 
B

Bjorn Abelli

...
I am handed a reference to a StreamWriter object.
I need to obtain the name of the underlying file
name that this writer is associated with. I can't
find a method to do so - am I missing something?

You're missing the fact that the StreamWriter doesn't neccessarily have to
be associated to a *file*. It can be used for any type of Stream. Hence,
there are no file-specific methods or properties for a StreamWriter.

However, if it really *is* associated to a file you could try something
like:

FileStream fs =
(FileStream)
yourStreamWriterObject.BaseStream;

string filename = fs.Name;


// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top