Streamwriter Path

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob

    #1

    Streamwriter Path

    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
  • Jon Skeet [C# MVP]

    #2
    Re: Streamwriter Path

    Rob <Rob@discussion s.microsoft.com > wrote:[color=blue]
    > 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.[/color]

    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.Ba seStream 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 - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Rob

      #3
      Re: Streamwriter Path

      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.Fil eStream)(((Syst em.IO.Stream)(_ JobLog.BaseStre am)))).Name;

      "Jon Skeet [C# MVP]" wrote:
      [color=blue]
      > Rob <Rob@discussion s.microsoft.com > wrote:[color=green]
      > > 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.[/color]
      >
      > 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.Ba seStream 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 - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      > If replying to the group, please do not mail me too
      >[/color]

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Streamwriter Path

        Rob <Rob@discussion s.microsoft.com > wrote:[color=blue]
        > 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.[/color]

        That still doesn't explain why you need to pass by reference. Did you
        read the link I posted?
        [color=blue]
        > 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.Fil eStream)(((Syst em.IO.Stream)[/color]
        (_JobLog.BaseSt ream)))).Name;

        Right. You don't need to cast to Stream, by the way - BaseStream is
        already declared to return a Stream.
        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        Working...