471,605 Members | 1,596 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

Streamwriter Path

Rob
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
Jan 3 '06 #1
3 2855
Rob <Ro*@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 3 '06 #2
Rob
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)(_JobLo g.BaseStream)))).Name;

"Jon Skeet [C# MVP]" wrote:
Rob <Ro*@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 3 '06 #3
Rob <Ro*@discussions.microsoft.com> wrote:
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.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Joecx | last post: by
5 posts views Thread by Brendan Miller | last post: by
2 posts views Thread by David Buchan | last post: by
7 posts views Thread by Eran.Yasso | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.