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
4
votes
1 answer

C# process.Kill does not immediately stop a process that is running a batch file

The issue: I am having an issue where I cannot immediately stop a batch file running inside an C# app (process) by using processName.Kill() In my project, the batch file will run several python scripts, taking around 30 minutes to complete in total.…
Alex23
  • 401
  • 4
  • 8
4
votes
1 answer

C# streamreader Read(char[] buffer, int index, int count) method fails to read from file after streamreader object Position is greater than buffersize

Update: I can confirm that the behaviors noted below were down to me doing something that I had not specified before which is that I was playing manually with the reader charPos property and therefore the question could be renamed: "How to screw up…
c123
  • 51
  • 1
  • 6
4
votes
1 answer

Streamwriter file is not being created

I'm using the code below to break apart a large text file into smaller files based on the logic you can see here. I'm getting an error on the File.WriteAllText line saying that tempfile doesn't exist. The flow is one header record, followed by…
imjustaboy
  • 326
  • 2
  • 12
4
votes
4 answers

Read from StreamReader in batches

I have been running into OutOfMemory Exceptions while trying to load an 800MB text file into a DataTable via StreamReader. I was wondering if there a way to load the DataTable from the memory stream in batches, ie, read the first 10,000 rows of the…
tt2
  • 45
  • 2
  • 5
4
votes
2 answers

C# StreamReader.ReadLine returning null before end of stream

I am using the SSH.NET library to implement a file system watcher on a remote linux server using the inotifywait command. Essentially it is a wrapper around: ssh myhost "inotifywait -m -e close_write --format '%:e %f' /dropzone" That command will…
Lucas
  • 14,227
  • 9
  • 74
  • 124
4
votes
2 answers

StreamReader and Writer from a list box

Question 1: whatever the user enters in the text box displays in the listbox but other text is showing up first then what the user enters shows up at the end. Question 2: my StreamReader / StreamWriter I keep getting 1601 error code to new to C# so…
T.J.
  • 43
  • 4
4
votes
2 answers

Read a .csv file in c# efficiently?

I'm reading huge csv files (about 350K lines by file) using this way: StreamReader readFile = new StreamReader(fi); string line; string[] row; readFile.ReadLine(); while ((line = readFile.ReadLine()) != null) { row =…
4
votes
3 answers

How to check if I have something to read on a StreamReader?

I need to check if my client has something in the buffer so I did not wait to write it. private async void ThreadMethod_ListenClient() { while (true) { if (ClientQueue.Count == 0) continue; Client…
4
votes
2 answers

How to properly handle blank, null, or empty lines in C#

I have some C# code that is working with a text file, and i can not seem to get it to work properly with blank or null (whitespace) lines. My code: while (!file.EndOfStream) { line = file.ReadLine(); bool…
Drifter64
  • 1,103
  • 3
  • 11
  • 30
4
votes
2 answers

How to read from a text file and store to array list in c#?

I'm trying to read a text file and store it's data to an array list. It's working without any errors. Inside of my text file is like this. 277.18 311.13 349.23 277.18 311.13 349.23 277.18 311.13 349.23 but in console output I can see this much of…
shona92
  • 63
  • 1
  • 12
4
votes
2 answers

Read HttpContent stream until a character limit using StreamReader

I am trying to convert the following code that reads the complete string response of a HttpContent into a string, to read only a certain maximum number of characters. Existing code: private static async Task
Romonov
  • 8,145
  • 14
  • 43
  • 55
4
votes
3 answers

to read in C# the millionth line

I have a very long text file. All rows have the same length. I want to read in C# the millionth line without first reading the previous 999999 lines because otherwise the program becomes too slow. How can I do?
4
votes
3 answers

C# StreamReader Encoding.UTF8 not working

I have a C# Project in Visual studio which download and parse XML file that contains Korean, Chinese and another unicode characters. For example for korean artist named Taeyang it produce XML like this : 태양 but it…
Mike
  • 1,231
  • 12
  • 17
4
votes
2 answers

C# removing last delimited field from a string split

I am a beginner c# programmer and just had a quick question on an application I am building. My process reads in multiple files with the purpose of stripping out specific records based on a 1 or 0 pipe delimited field in the text file. It is the…
user3494110
  • 417
  • 2
  • 9
  • 25
4
votes
3 answers

StreamReader Gets Stuck on Large Files

I'm trying to read a 50GB text file with a streamreader. It doesn't even get to reading the file, because my program freezes on the second line(of code, not the file). string filename = @"C:\wikipedia\dump.xml"; StreamReader wikipediaReader = new…
user1599078
  • 167
  • 1
  • 3
  • 9