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
19
votes
2 answers

C# Stream.Read with timeout

I have this streamreader: Boolean read = false; while (wline!="exit") { while (!read || streamReader.Peek() >= 0) { read = true; …
Tobia
  • 9,165
  • 28
  • 114
  • 219
17
votes
4 answers

search text file using c# and display the line number and the complete line that contains the search keyword

I require help to search a text file (log file) using c# and display the line number and the complete line that contains the search keyword.
Chitresh
  • 405
  • 1
  • 6
  • 16
17
votes
3 answers

Is it safe to use 'using' instead of closing a WebResponse and StreamReader

Currently I've implemented a simple helper method for HttpWebRequest called GetResponse(url). Currently I'm manually closing the WebResponse and StreamReader after reading the result. I'm then returning said result like so: // construct the…
Richard
  • 8,110
  • 3
  • 36
  • 59
16
votes
2 answers

Loading a .csv file into dictionary, I keep getting the error "cannot convert from 'string[]' to 'string'"

I've used streamreader to read in a .csv file, then i need to split the values and put them into a dictionary. so far i have: namespace WindowsFormsApplication2 { public partial class Form1 : Form { Dictionary dict = new…
jesusjuice
  • 163
  • 1
  • 1
  • 8
15
votes
7 answers

Httplistener and file upload

I am trying to retrieve an uploaded file from my webserver. As the client sends its files through a webform (random files), I need to parse the request to get the file out and to process it further on. Basically, the code goes…
cecemel
  • 616
  • 2
  • 7
  • 22
15
votes
4 answers

simultaneous read-write a file in C#

I have a file containing data that I'd like to monitor changes to, as well as add changes of my own. Think like "Tail -f foo.txt". Based on this thread, it looks like I should just create a filestream, and pass it both to a writer and reader. …
tbischel
  • 6,337
  • 11
  • 51
  • 73
14
votes
5 answers

StreamReader ReadToEnd() returns empty string on first attempt

I know this question has been asked before on Stackoverflow, but could not find an explanation. When I try to read a string from a compressed byte array I get an empty string on the first attempt, on the second I succed and get the string. Code…
Victor Cassel
  • 201
  • 1
  • 2
  • 5
14
votes
3 answers

What is the purpose of StreamReader when Stream.Read() exists?

This has been bugging me. I know Stream is an abstract class and therefore can't be instantiated but it has classes that are derived from it. Why is there a StreamReader class and a Stream.Read() method (and vice verse for StreamWriter and…
Bagofsheep
  • 159
  • 1
  • 1
  • 8
14
votes
6 answers

Adding a Line to the Middle of a File with .NET

Hello I am working on something, and I need to be able to be able to add text into a .txt file. Although I have this completed I have a small problem. I need to write the string in the middle of the file more or less. Example: Hello my name is…
Brandon
  • 141
  • 1
  • 1
  • 4
14
votes
7 answers

Is StreamReader.Readline() really the fastest method to count lines in a file?

While looking around for a while I found quite a few discussions on how to figure out the number of lines in a file. For example these three: c# how do I count lines in a textfile Determine the number of lines within a text file How to count lines…
sergeidave
  • 662
  • 4
  • 11
  • 23
14
votes
6 answers

How To Use HttpWebRequest/Response To Download A Binary (.exe) File From A Web Server?

I am writing a program that needs to download an .exe file from a website and then save it to the hard drive. The .exe is stored on my site and it's url is as follows (it's not the real uri just one I made up for the purpose of this…
Jan Tacci
  • 3,131
  • 16
  • 63
  • 83
13
votes
8 answers

Extracting the first 10 lines of a file to a string

public void get10FirstLines() { StreamReader sr = new StreamReader(path); String lines = ""; lines = sr.readLine(); } How can I get the first 10 lines of the file in the string?
user2891133
  • 309
  • 2
  • 4
  • 13
13
votes
5 answers

StreamReader complains that file does not exist, but it does

I have an application that is localized for use across Europe. I have a menu option that loads a file from disk. This operation works fine on my dev machine but does not work on the virtual machine I use to test other operating systems _ e.g…
Kildareflare
  • 4,590
  • 5
  • 51
  • 65
12
votes
1 answer

StreamReader is unable to correctly read extended character set (UTF8)

I am having an issue where I am unable to read a file that contains foreign characters. The file, I have been told, is encoded in UTF-8 format. Here is the core of my code: using (FileStream fileStream = fileInfo.OpenRead()) { using…
PolandSpring
  • 2,664
  • 7
  • 26
  • 35
12
votes
4 answers

How to get HttpRequest body in .net core?

I want to get Http Request body in .net core , I used this code: using (var reader = new StreamReader(req.Body, Encoding.UTF8)) { bodyStr = reader.ReadToEnd(); } req.Body.Position = 0 But I got this error: System.ObjectDisposedException:…
rayan periyera
  • 123
  • 1
  • 1
  • 6