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

How to take a stringbuilder and convert it to a streamReader?

How to take a stringbuilder and convert it to a stream? SO my stringbuilder has to be converted into a : StreamReader stream = ???? Update I tried using a stringreader like: StringReader sr = new StringReader(sb.ToString()); StreamReader stream =…
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
7
votes
4 answers

Optimizing Listview in C# for large files

I have a C# program that is pulling in a .csv file that is approx. 42,000 lines long. All data in the file is stored as follows: Zipcode,City,State I am pulling all the information into three different columns in a listview. Currently this data…
Mark P.
  • 1,827
  • 16
  • 37
7
votes
5 answers

How to read the Website content in c#?

I want to read the website text without html tags and headers. i just need the text displayed in the web browser. i don't need like this bla bla bla bla i just need the text "bla bla bla bla". I have used the…
Azeem Akram
  • 223
  • 6
  • 9
  • 21
7
votes
1 answer

What is the difference between Read() and ReadBlock() calls on TextReader?

The title more or less says it all. The calls are documented: Here for TextReader.Read Method (Char[], Int32, Int32) and Here for TextReader.ReadBlock() with the same argument types. I want to extract a portion of a byte array, for which I make up…
Marcel
  • 15,039
  • 20
  • 92
  • 150
6
votes
2 answers

StreamReader and binary data

I have this text file what contains different fields. Some fields may contain binary data. I need to get all the data in the file but right now when using StreamReader then it wont read the binary data block and data what comes after that. What…
hs2d
  • 6,027
  • 24
  • 64
  • 103
6
votes
1 answer

Why are null bytes appearing? Even after "sanitizing" the stream

I've been tying to figure why null bytes are appearing in certain strings. Example…
reticentroot
  • 3,612
  • 2
  • 22
  • 39
6
votes
2 answers

C# Error reading JObject from JsonReader. Path '', line 0, position 0

I'm trying to parse my json with the code below. I get the error: Error reading JObject from JsonReader. Path '', line 0, position 0. I thought this might be because my JSON was malformed, so I have output it, and it appears to be ok: { …
hlh3406
  • 1,382
  • 5
  • 29
  • 46
6
votes
2 answers

Read text file line by line using timer

StreamReader sr = new StreamReader("C:/CR EZ Test/Log.txt"); //use with IF private void timer2_Tick(object sender, EventArgs e) { if ((line = sr.ReadLine()) != null) { //FileStream fs = File.Open("C:/CR EZ Test/Log.txt",…
RatherLogical
  • 310
  • 4
  • 15
6
votes
3 answers

type or namespace name "StreamReader" could not be found

I am working on StreamReader in C#. I am getting the error "The type or namespace name "StreamReader" could not be found" I have no idea what I am doing wrong. using System.IO; using System; class Perfect { static void Main() { string…
maddddie123
  • 185
  • 1
  • 1
  • 10
6
votes
2 answers

StreamWriter Not working in C#

This piece of code worked perfectly in VS 2010. Now that I have VS 2013 it no longer writes to the file. It doesn't error or anything. (I get an alert in Notepad++ stating that the file has been updated, but there is nothing written.) It all looks…
tvoytko
  • 93
  • 1
  • 6
6
votes
1 answer

GZipStream with StreamReader.ReadLine only reads first line

I have a gzip file containing a txt file that needs to be cleaned up. I would like to read from the GZipped file line by line and then write the cleaned content to an output GZIP file all in one shot like this: void ExtractAndFix(string…
bruiseruser
  • 343
  • 4
  • 12
6
votes
3 answers

Stream read line

I have a stream reader line by line (sr.ReadLine()). My code counts the line-end with both line endings \r\n and/or \n. StreamReader sr = new System.IO.StreamReader(sPath, enc); while (!sr.EndOfStream) { //…
procma
  • 1,174
  • 3
  • 14
  • 24
6
votes
4 answers

Fast way to change txt file and save it

I have txt file(65mb)i need to read line by line and change each line, For example i have many lines User=value Password=value Phone=123456789 User=value Password=value Phone=123456789 User=value Password=value Phone=123456789 and i need to…
user3567884
  • 213
  • 1
  • 6
  • 19
6
votes
3 answers

Stream Reader process cannot access file because its in use by another process

My application parses log files but when trying to parse the current day's file I get an error stating that the file is being used by another process. This log file is currently being written to and can be accessed through notepad but not through…
SlopTonio
  • 1,105
  • 1
  • 16
  • 39
6
votes
2 answers

Deserialize multiple objects from stream by json.net

Greatings! I need to deserialize a file of different objects serialized as json. Here is the resulting file: { "Number": 1, "Description": "Run version with strategy data", "Context": "NA" }[ { "N": 0.0, "T": 2.0, "Adc": [ …
PanCotzky
  • 564
  • 2
  • 5
  • 12