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
848
votes
24 answers

How to read embedded resource text file

How do I read an embedded resource (text file) using StreamReader and return it as a string? My current script uses a Windows form and textbox that allows the user to find and replace text in a text file that is not embedded. private void…
Me.Close
  • 8,531
  • 4
  • 17
  • 6
196
votes
7 answers

Should I call Close() or Dispose() for stream objects?

Classes such as Stream, StreamReader, StreamWriter etc implements IDisposable interface. That means, we can call Dispose() method on objects of these classes. They've also defined a public method called Close(). Now that confuses me, as to what…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
185
votes
7 answers

How to find and replace text in a file

My code so far StreamReader reading = File.OpenText("test.txt"); string str; while ((str = reading.ReadLine())!=null) { if (str.Contains("some text")) { StreamWriter write = new StreamWriter("test.txt"); } } I know how…
Win Coder
  • 6,628
  • 11
  • 54
  • 81
182
votes
7 answers

Does disposing streamreader close the stream?

I am sending a stream to methods to write on, and in those methods I am using a binary reader/wrtier. When the reader/writer gets disposed, either by using or just when it is not referenced, is the stream closed as well?? I would send a…
Nefzen
  • 7,819
  • 14
  • 36
  • 34
169
votes
4 answers

Converting file into Base64String and back again

The title says it all: I read in a tar.gz archive like so break the file into an array of bytes Convert those bytes into a Base64 string Convert that Base64 string back into an array of bytes Write those bytes back into a new tar.gz file I can…
darkpbj
  • 2,892
  • 4
  • 22
  • 32
111
votes
13 answers

Reading large text files with streams in C#

I've got the lovely task of working out how to handle large files being loaded into our application's script editor (it's like VBA for our internal product for quick macros). Most files are about 300-400 KB which is fine loading. But when they go…
Nicole Lee
  • 1,133
  • 2
  • 8
  • 4
82
votes
2 answers

How do you send an HTTP Get Web Request in Python?

I am having trouble sending data to a website and getting a response in Python. I have seen similar questions, but none of them seem to accomplish what I am aiming for. This is my C# code I'm trying to port to Python: static void Request(Uri…
voice
  • 1,326
  • 1
  • 11
  • 16
77
votes
6 answers

An elegant way to consume (all bytes of a) BinaryReader?

Is there an elegant to emulate the StreamReader.ReadToEnd method with BinaryReader? Perhaps to put all the bytes into a byte array? I do this: read1.ReadBytes((int)read1.BaseStream.Length); ...but there must be a better way.
SamFisher83
  • 3,937
  • 9
  • 39
  • 52
62
votes
9 answers

How to read a large (1 GB) txt file in .NET?

I have a 1 GB text file which I need to read line by line. What is the best and fastest way to do this? private void ReadTxtFile() { string filePath = string.Empty; filePath = openFileDialog1.FileName; if…
Jeevan Bhatt
  • 5,881
  • 18
  • 54
  • 82
58
votes
5 answers

How do I open an already opened file with a .net StreamReader?

I have some .csv files which I'm using as part of a test bench. I can open them and read them without any problems unless I've already got the file open in Excel in which case I get an IOException: System.IO.IOException : The process cannot access…
Jon Cage
  • 36,366
  • 38
  • 137
  • 215
48
votes
6 answers

Memory Leak using StreamReader and XmlSerializer

I've been googling for the past few hours and trying different things but can't seem to the bottom of this.... When I run this code, the memory usage continuously grows. while (true) { try { foreach (string sym in stringlist) …
Alex999
  • 502
  • 2
  • 5
  • 8
47
votes
1 answer

StreamReader vs BinaryReader?

Both StreamReader and BinaryReader can be used to get data from binary file ( for example ) BinaryReader : using (FileStream fs = File.Open(@"c:\1.bin",FileMode.Open)) { byte[] data = new…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
42
votes
6 answers

Do I need to explicitly close the StreamReader in C# when using it to load a file into a string variable?

Example: variable = new StreamReader( file ).ReadToEnd(); Is that acceptable?
Matt Ralston
  • 666
  • 1
  • 6
  • 15
34
votes
5 answers

Will a using clause close this stream?

I've apparently worked myself into a bad coding habit. Here is an example of the code I've been writing: using(StreamReader sr = new StreamReader(File.Open("somefile.txt", FileMode.Open))) { //read file } File.Move("somefile.txt",…
scottm
  • 27,829
  • 22
  • 107
  • 159
34
votes
1 answer

Difference between StreamReader.Read and StreamReader.ReadBlock

The documentation simply says ReadBlock is "a blocking version of Read" but what does that mean? Someone else has asked the question before but, huh? http://www.pcreview.co.uk/forums/thread-1385785.php The guy answering said Basically, it means…
J M
  • 1,877
  • 2
  • 20
  • 32
1
2 3
99 100