File to Memory stream

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

    File to Memory stream

    I have code that opens a file to a textreader then I load the reader into
    memory through the System.Text.Enc oding.UTF8.GetB ytes. Is there a way to
    open a file directly into memory?


  • Morten Wennevik

    #2
    Re: File to Memory stream

    Hi Robert,

    With Framework 2.0 you will get File.ReadAll/ReadAllLines/ReadAllBytes/ReadAllText,

    Until then, the simplest way is to use the FileStream

    FileStream fs = File.OpenRead(p ath);
    byte[] b = new byte[fs.Length];
    fs.Read(b, 0, b.Length);
    fs.Close();


    On Wed, 25 May 2005 00:23:13 +0200, Robert Strickland <bstrickland@co mporium.net> wrote:
    [color=blue]
    > I have code that opens a file to a textreader then I load the reader into
    > memory through the System.Text.Enc oding.UTF8.GetB ytes. Is there a way to
    > open a file directly into memory?
    >
    >
    >[/color]



    --
    Happy coding!
    Morten Wennevik [C# MVP]

    Comment

    • Adam Clauss

      #3
      Re: File to Memory stream

      What do you mean by "directly into memory"?

      You could just use a FileStream and call Read() if that's all you want.

      --
      Adam Clauss
      cabadam@tamu.ed u

      "Robert Strickland" <bstrickland@co mporium.net> wrote in message
      news:Oc27g6KYFH A.2128@TK2MSFTN GP14.phx.gbl...[color=blue]
      >I have code that opens a file to a textreader then I load the reader into
      > memory through the System.Text.Enc oding.UTF8.GetB ytes. Is there a way to
      > open a file directly into memory?
      >
      >[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: File to Memory stream

        Robert Strickland <bstrickland@co mporium.net> wrote:[color=blue]
        > I have code that opens a file to a textreader then I load the reader into
        > memory through the System.Text.Enc oding.UTF8.GetB ytes. Is there a way to
        > open a file directly into memory?[/color]

        Just use a FileStream instead. Note that using a TextReader like that
        won't always give you back the original bytes - not every file is a
        valid UTF-8 file.

        --
        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

        • Robert Strickland

          #5
          Re: File to Memory stream

          Thanks

          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          news:MPG.1cfe28 d52bf2c8fa98c1b c@msnews.micros oft.com...[color=blue]
          > Robert Strickland <bstrickland@co mporium.net> wrote:[color=green]
          >> I have code that opens a file to a textreader then I load the reader into
          >> memory through the System.Text.Enc oding.UTF8.GetB ytes. Is there a way to
          >> open a file directly into memory?[/color]
          >
          > Just use a FileStream instead. Note that using a TextReader like that
          > won't always give you back the original bytes - not every file is a
          > valid UTF-8 file.
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet
          > If replying to the group, please do not mail me too[/color]


          Comment

          • Robert Strickland

            #6
            Re: File to Memory stream

            Thanks

            "Morten Wennevik" <MortenWennevik @hotmail.com> wrote in message
            news:op.sraxhrm fklbvpo@stone.. .[color=blue]
            > Hi Robert,
            >
            > With Framework 2.0 you will get
            > File.ReadAll/ReadAllLines/ReadAllBytes/ReadAllText,
            >
            > Until then, the simplest way is to use the FileStream
            >
            > FileStream fs = File.OpenRead(p ath);
            > byte[] b = new byte[fs.Length];
            > fs.Read(b, 0, b.Length);
            > fs.Close();
            >
            >
            > On Wed, 25 May 2005 00:23:13 +0200, Robert Strickland
            > <bstrickland@co mporium.net> wrote:
            >[color=green]
            >> I have code that opens a file to a textreader then I load the reader into
            >> memory through the System.Text.Enc oding.UTF8.GetB ytes. Is there a way to
            >> open a file directly into memory?
            >>
            >>
            >>[/color]
            >
            >
            >
            > --
            > Happy coding!
            > Morten Wennevik [C# MVP][/color]


            Comment

            • Robert Strickland

              #7
              Re: File to Memory stream

              Thanks

              "Adam Clauss" <cabadam@nospam .tamu.edu> wrote in message
              news:ePULtKLYFH A.2508@TK2MSFTN GP15.phx.gbl...[color=blue]
              > What do you mean by "directly into memory"?
              >
              > You could just use a FileStream and call Read() if that's all you want.
              >
              > --
              > Adam Clauss
              > cabadam@tamu.ed u
              >
              > "Robert Strickland" <bstrickland@co mporium.net> wrote in message
              > news:Oc27g6KYFH A.2128@TK2MSFTN GP14.phx.gbl...[color=green]
              >>I have code that opens a file to a textreader then I load the reader into
              >> memory through the System.Text.Enc oding.UTF8.GetB ytes. Is there a way to
              >> open a file directly into memory?
              >>
              >>[/color]
              >
              >[/color]


              Comment

              Working...