Stream Writer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Charles A. Lackman

    #1

    Stream Writer

    Hello,

    I have created a MemoryStream that is holding Binary Data. How do I get the
    Data out of the Memory Stream into a file on my hard drive. All the
    examples I have seen for a MemoryStream write the contents to the console.

    Thanks
    Chuck


  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Stream Writer

    Chuck,
    You can use MemoryStream.To Array to get the MemoryStream as an array of
    bytes.

    You can then use a FileStream to write this array of bytes to a file.

    Alternatively you could read the MemoryStream from the beginning writing the
    data to a FileStream...

    However if your goal is to write Binary Data to a file, why not simply open
    a FileStream directly instead of using an intermediate MemoryStream?

    Hope this helps
    Jay

    "Charles A. Lackman" <Charles@Create ItSoftware.net> wrote in message
    news:eaH2vRUtEH A.3772@TK2MSFTN GP10.phx.gbl...[color=blue]
    > Hello,
    >
    > I have created a MemoryStream that is holding Binary Data. How do I get
    > the Data out of the Memory Stream into a file on my hard drive. All the
    > examples I have seen for a MemoryStream write the contents to the console.
    >
    > Thanks
    > Chuck
    >[/color]


    Comment

    • Arne Janning

      #3
      Re: Stream Writer

      Hi Charles!

      "Charles A. Lackman" schrieb[color=blue]
      > I have created a MemoryStream that is holding Binary Data. How do I get
      > the Data out of the Memory Stream into a file on my hard drive. All the
      > examples I have seen for a MemoryStream write the contents to the console.[/color]

      In addition to the possibilities Jay listed you can use the
      MemoryStream.Wr iteTo()-Method:

      Dim buffer As Byte() = New Byte() {1, 2, 3, 4, 5}
      Dim ms As MemoryStream = New MemoryStream(bu ffer)
      Dim st As FileStream = New FileStream( _
      "C:\test.bi n", FileMode.Create )
      ms.WriteTo(st)
      st.Close()
      ms.Close()


      Cheers

      Arne Janning


      Comment

      • Charles A. Lackman

        #4
        Re: Stream Writer

        Hello I am retrieve binary data from a web service and need to write it to
        disk. It will be a setup file or replacement files I have made for
        applications.

        This is where I am lost

        the memory stream is filled.

        ms.Seek(0, SeekOrigin.Begi n)
        Dim MyWriter as StreamWriter
        MyWriter = New StreamWriter("C :\Test.Setup")

        ms.read
        mywriter.write( ms)

        ms.close
        mywriter.close
        This does not work, what am I missing,

        Thanks
        Chuck


        "Charles A. Lackman" <Charles@Create ItSoftware.net> wrote in message
        news:eaH2vRUtEH A.3772@TK2MSFTN GP10.phx.gbl...[color=blue]
        > Hello,
        >
        > I have created a MemoryStream that is holding Binary Data. How do I get
        > the Data out of the Memory Stream into a file on my hard drive. All the
        > examples I have seen for a MemoryStream write the contents to the console.
        >
        > Thanks
        > Chuck
        >[/color]


        Comment

        • Charles A. Lackman

          #5
          Re: Stream Writer

          Thank You, Thank You, Thank You,

          Chuck
          :)
          "Charles A. Lackman" <Charles@Create ItSoftware.net> wrote in message
          news:eaH2vRUtEH A.3772@TK2MSFTN GP10.phx.gbl...[color=blue]
          > Hello,
          >
          > I have created a MemoryStream that is holding Binary Data. How do I get
          > the Data out of the Memory Stream into a file on my hard drive. All the
          > examples I have seen for a MemoryStream write the contents to the console.
          >
          > Thanks
          > Chuck
          >[/color]


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Stream Writer

            Charles A. Lackman <Charles@Create ItSoftware.net> wrote:[color=blue]
            > Hello I am retrieve binary data from a web service and need to write it to
            > disk. It will be a setup file or replacement files I have made for
            > applications.
            >
            > This is where I am lost
            >
            > the memory stream is filled.
            >
            > ms.Seek(0, SeekOrigin.Begi n)
            > Dim MyWriter as StreamWriter
            > MyWriter = New StreamWriter("C :\Test.Setup")
            >
            > ms.read
            > mywriter.write( ms)
            >
            > ms.close
            > mywriter.close
            > This does not work, what am I missing,[/color]

            One point here, apart from what the others have said: if you've got
            binary data, you shouldn't use StreamWriter. Writers and Readers are
            for text data. Streams are for binary data. Just use FileStream.

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            Working...