Getting the underlying filename of a System.IO.StreamWriter

  • Thread starter Thread starter Tom Jones
  • Start date Start date
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
 
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
 
...
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
 
Back
Top