Questions tagged [streamreader]

StreamReader is a C# class designed for character input in a particular encoding, it can be used for reading lines of information from a standard text file. By default, a StreamReader is not thread safe.

Implements a TextReader that reads characters from a byte stream in a particular encoding.

StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. StreamReader can be used for reading lines of information from a standard text file.

StreamReader defaults to UTF-8 encoding unless specified otherwise, instead of defaulting to the ANSI code page for the current system. UTF-8 handles Unicode characters correctly and provides consistent results on localized versions of the operating system. If you get the current character encoding using the CurrentEncoding property, the value is not reliable until after the first Read method, since encoding auto detection is not done until the first call to a Read method.

By default, a StreamReader is not thread safe. See TextReader.Synchronized for a thread-safe wrapper.

Official Microsoft documentation

1931 questions
12
votes
4 answers

What are the default values for StreamReader?

I need to use this constructor public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen) in order to set leaveOpen to true. And in order to do that I need to set the other parameters…
Nuri Tasdemir
  • 9,720
  • 3
  • 42
  • 67
12
votes
6 answers

StreamReader.ReadLine will hang in an infinite loop

I have a simple program to read a file using the StreamReader and process it line by line. But the file I am reading may sometimes locate in a network folder. I came across while doing some testing with such a file, that if the network connection…
Asanka
  • 539
  • 3
  • 6
  • 14
11
votes
7 answers

How to know position(linenumber) of a streamreader in a textfile?

an example (that might not be real life, but to make my point) : public void StreamInfo(StreamReader p) { string info = string.Format( "The supplied streamreaer read : {0}\n at line {1}", p.ReadLine(), …
Peter
  • 47,963
  • 46
  • 132
  • 181
11
votes
3 answers

How to read huge CSV file with 29 million rows of data using .net

I have a huge .csv file, to be specific a .TAB file with 29 million rows and the file size is around 600 MB. I would need to read this into an IEnumerable collection. I have tried CsvHelper, GenericParser, and few other solutions but always ending…
Lee
  • 139
  • 1
  • 1
  • 8
11
votes
2 answers

StreamReader and Portable Class Library

I am writing a ConfigManager class using Portable Class Libraries. PCL supports StreamReader and StreamWriter classes that I want to use, but the PCL version of those classes do not support passing in a string during construction. PCL also does not…
11
votes
4 answers

C# - StreamReader.ReadLine does not work properly!

Simply I have been trying to implement what BufferedStreamReader does in Java. I have a socket stream open and just want to read it in a line-oriented fashion - line by line. I have the following server-code. while (continueProcess) { …
Aleyna
  • 1,857
  • 4
  • 20
  • 27
11
votes
3 answers

Unable to read data from the transport connection: The connection was closed error in console application

I have this code in console application and it runs in a loop try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(search); request.Headers.Add("Accept-Language", "de-DE"); request.Method = "GET"; request.Accept…
Darshana
  • 2,462
  • 6
  • 28
  • 54
11
votes
3 answers

Is there a better way to count the lines in a text file?

Below is what I've been using. While it does work, my program locks up when trying to count a rather large file, say 10,000 or more lines. Smaller files run in no time. Is there a better or should I say faster way to count the lines in a text…
Muhnamana
  • 1,014
  • 13
  • 34
  • 57
10
votes
2 answers

Am I really forced to ReadToEnd() a StreamReader reading an Ionic.Zlib.GZipStream?

I am using the following code to uncompress a GZipStream (using DotNetZip library), where fs is a filestream pointing to a gz file (with FileMode.Open, FileAccess.Read, FileShare.ReadWrite): using (var gz = new GZipStream(fs,…
Erwin Mayer
  • 18,076
  • 9
  • 88
  • 126
10
votes
5 answers

StreamReader and reading an XML file

I get a response from a web-server using StreamReader... now I want to parse this response (it's an XML document file) to get its values, but every time I try to do it I get a error: Root element is missing. If I read the same XML file directly, the…
Badr Hari
  • 8,114
  • 18
  • 67
  • 100
10
votes
6 answers

Why disposing StreamReader makes a stream unreadable?

I need to read a stream two times, from start to end. But the following code throws an ObjectDisposedException: Cannot access a closed file exception. string fileToReadPath = @""; using (FileStream fs = new FileStream(fileToReadPath,…
Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
10
votes
1 answer

StreamReader to Read Range of lines

Lets say I have a File and want to read the lines, it is: while( !streamReader.EndOfStream ) { var line = streamReader.ReadLine( ); } How do I read only a range of lines? Like readlines from 10 to 20 only.
user1702369
  • 1,089
  • 2
  • 13
  • 31
10
votes
2 answers

Reading large file in chunks c#

I want to read very large file (4GBish) chunk by chunk. I am currently trying to use a StreamReader and the Read() read method. The syntax is: sr.Read(char[] buffer, int index, int count) Because the index is an int it will overflow in my case.…
TJF
  • 1,081
  • 1
  • 12
  • 26
10
votes
1 answer

StreamReader get and set position

i simply want to read a large CSV-File and save the Stream position in a list. After that i have to read the position from the list and set the position of the Streamreader to that char and read a line!! But after i read the first line and return…
coolerfarmer
  • 216
  • 1
  • 4
  • 12
10
votes
1 answer

StreamReader from MemoryStream UTF8 Encoding

I want to open a XML file (from an zip archive) in a MemoryStream and create a StreamReader form this stream to put it into a GridView. I use this code : MemoryStream ms = new MemoryStream(); entry.Extract(ms); StreamReader reader = new…
flow
  • 4,828
  • 6
  • 26
  • 41