File to Memory stream

  • Thread starter Thread starter Robert Strickland
  • Start date Start date
R

Robert Strickland

I have code that opens a file to a textreader then I load the reader into
memory through the System.Text.Encoding.UTF8.GetBytes. Is there a way to
open a file directly into memory?
 
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(path);
byte[] b = new byte[fs.Length];
fs.Read(b, 0, b.Length);
fs.Close();
 
What do you mean by "directly into memory"?

You could just use a FileStream and call Read() if that's all you want.
 
Robert Strickland said:
I have code that opens a file to a textreader then I load the reader into
memory through the System.Text.Encoding.UTF8.GetBytes. Is there a way to
open a file directly into memory?

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

Morten Wennevik said:
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(path);
byte[] b = new byte[fs.Length];
fs.Read(b, 0, b.Length);
fs.Close();


I have code that opens a file to a textreader then I load the reader into
memory through the System.Text.Encoding.UTF8.GetBytes. Is there a way to
open a file directly into memory?
 
Thanks

Jon Skeet said:
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.
 
Back
Top