Streamwriter Path

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a streamwriter that may get closed. The writer is passed into another
class by ref and is consumed there. What can happen is that the writer might
get closed by the spawning class.

Is there a way to extract the path of the streamwriter passed in by ref? I
don't see a property for it, but there might be a work around someone has.

Let me know!

Thanks!

Rob
 
Rob said:
I have a streamwriter that may get closed. The writer is passed into another
class by ref and is consumed there. What can happen is that the writer might
get closed by the spawning class.

Is there a way to extract the path of the streamwriter passed in by ref? I
don't see a property for it, but there might be a work around someone has.

No, because there may not *be* a path. (What would the StreamWriter for
a writer to a MemoryStream be?)

What you *can* do is use the StreamWriter.BaseStream to get the
underlying stream, and then if that's a FileStream you can use
FileStream.Name to access the path.

I'm not sure why you've mentioned twice that the StreamWriter is being
passed by reference - it's irrelevant here, even if it's true. I
*suspect* that it's not actually being passed by reference, however -
or at least doesn't need to be. It's worth understanding parameter
passing thoroughly. See
http://www.pobox.com/~skeet/csharp/parameters.html for more
information.
 
Thanks Jon,

That is what I ended up doing. To clairfy, the ref to the streamwriter was
to originally use an already open writer from the spawning app. A new tread
for a seporate class is later launched and the writer is passed by ref to the
that class.

Now I am just closing the writer, then opening it using the path of the
filestream that was passed in.

Simply put - _jobLogPath =
((System.IO.FileStream)(((System.IO.Stream)(_JobLog.BaseStream)))).Name;
 
Rob said:
That is what I ended up doing. To clairfy, the ref to the streamwriter was
to originally use an already open writer from the spawning app. A new tread
for a seporate class is later launched and the writer is passed by ref to the
that class.

That still doesn't explain why you need to pass by reference. Did you
read the link I posted?
Now I am just closing the writer, then opening it using the path of the
filestream that was passed in.

Simply put - _jobLogPath =
((System.IO.FileStream)(((System.IO.Stream)
(_JobLog.BaseStream)))).Name;

Right. You don't need to cast to Stream, by the way - BaseStream is
already declared to return a Stream.
 
Back
Top