Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read data in FileStream into a generic Stream

Tags:

c#

stream

What's the most efficient way to read a stream into another stream? In this case, I'm trying to read data in a Filestream into a generic stream. I know I could do the following:
1. read line by line and write the data to the stream
2. read chunks of bytes and write to the stream
3. etc

I'm just trying to find the most efficient way.

Thanks

like image 959
James Avatar asked Sep 24 '08 19:09

James


People also ask

How to read stream data in c#?

C# StreamReader ReadLineThe ReadLine method reads a line of characters from the current stream and returns the data as a string. It returns null if the end of the input stream is reached. The example reads the whole file line by line by using the ReadLine method. We use a while loop to read all lines.

How to convert text file to stream in c#?

using var fs = new FileStream(path, FileMode. Open, FileAccess. Read); The FileStream class provides a Stream for a file, supporting both synchronous and asynchronous read and write operations.

How to read file into MemoryStream in c#?

You can simply call File. OpenRead to get a FileStream containing the file. If you really want the file to be in a MemoryStream, you can call CopyTo to copy the FileStream to a MemoryStream.

Which method do you call to read the phrases from the database stream?

The StreamReader is used for reading string or large sentences from a file. The StreamReader also uses the TextReader class as its base class and then offers methods like Reading and ReadLine to read data from the stream.


1 Answers

Stephen Toub discusses a stream pipeline in his MSDN .NET matters column here. In the article he describes a CopyStream() method that copies from one input stream to another stream. This sounds quite similar to what you're trying to do.

like image 110
Jeff Stong Avatar answered Sep 24 '22 20:09

Jeff Stong