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

Last character of text file is not retrieved with System.IO

I tried to write a text file using OutputStream.Write but, I've noticed that the last character of the file is not being sent. Whether the file is 6KB or 242KB, the last character is…
Syed Abdul Qadeer
  • 455
  • 1
  • 6
  • 14
4
votes
3 answers

StreamWriter/StreamReader File In Use By Another Process

I have a StreamWriter object to write to a log file, it is initialised as follows: StreamWriter streamWriter = File.AppendText(GetLogFilePath()); streamWriter.AutoFlush = true; Later in my code, I need to close the log file, and then read in part…
HaemEternal
  • 2,229
  • 6
  • 31
  • 50
4
votes
3 answers

How to edit a text file in c# with StreamWriter?

I have a method to edit a word from a text file file and display it on the command prompt. Now I an trying to make a method to edit a word from a text file and write it to a new file. I would also like to mention that I cannot use the File or Regex…
user1849989
  • 147
  • 2
  • 2
  • 10
4
votes
4 answers

StreamReading a file immediately after StreamWriting to that same file

Given this example code: 1: using (StreamWriter sw = new StreamWriter(@"C:\file.txt")) 2: { 3: sw.WriteLine(great_string); 4: } 5: 6: using (StreamReader sr = new StreamReader(@"C:\file.txt")) 7: { 8: sr.ReadToEnd(); 9: } Is…
Brian Snow
  • 1,133
  • 1
  • 12
  • 23
4
votes
1 answer

Lines of a StreamReader to an array of string

I want to get a string[] assigned with a StreamReader. Like: try{ StreamReader sr = new StreamReader("a.txt"); do{ str[i] = sr.ReadLine(); i++; }while(i < 78); } catch (Exception ex){ …
whoone
  • 533
  • 3
  • 7
  • 16
4
votes
2 answers

AES Decryption Exception change

I have an AES Cryptography wrapper and unit tests that has been working for over a year. Now after installing VS 2012 (or maybe an update to .net Framework 4) the unit tests do not pass. The streamreader block was throwing a CryptographicException…
Jimmy James
  • 381
  • 1
  • 3
  • 6
4
votes
3 answers

filestream not finishing writing to to file

What the program does is goes out to a webpage, reads the source line by line, strips out the html tags/code, and then writes the actual text/information to a text file. Because I want the text file to only contain the data/information I want and…
Will Carr
  • 51
  • 1
  • 4
4
votes
5 answers

Parse StreamReader using regex efficiently

I have the variable StreamReader DebugInfo = GetDebugInfo(); var text = DebugInfo.ReadToEnd(); // takes 10 seconds!!! because there are a lot of students text equals: Antonio
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
4
votes
5 answers

C# Detecting null characters in a string array

I am reading in from a text file using StreamReader into a string array text[]. One of the lines in the textfile is being read in as "\0" in positions 1 -> 20 of the array. How would I go about detecting this null character and ignoring this…
Daniel Flannery
  • 1,166
  • 8
  • 23
  • 51
4
votes
4 answers

C# Streamreader "The type or namespace name could not be found" error, but I've got the System.IO namespace

I'm brand new to C#, but I think I have the correct "using" statements here, so I presume the problem is somewhere in my class structure or syntax? I'm getting the "The type or namespace name 'Textreader' could not be found" error. Thank…
Kristi Simonson
  • 515
  • 1
  • 6
  • 22
3
votes
5 answers

C# how to separate by paragraph?

so I know a paragraph is char 10 + char 13 I do: streamreader sr = new streamreader(); string s = sr.ReadToEnd(); string s1 = s.Replace((char)10, "*"); string s2 = s1.Replace((char)13, "*"); Now it changed paragraphs to two ** but how do I split…
user1243565
  • 91
  • 1
  • 2
  • 3
3
votes
3 answers

Remove last x lines from a streamreader

I need to read in all but the last x lines from a file to a streamreader in C#. What is the best way to do this? Many Thanks!
3
votes
3 answers

Is FileStream very slow?

I was trying to copy a 5 GB ISO file onto a 32 GB flash drive with 29 GB of free space. Windows 7 refused to let me drag-and-drop the file onto the flash drive, reporting the file was too large for the destination file system. I eventually learned…
user153923
3
votes
3 answers

Moving the file cursor up lines?

I've googled this like crazy, and I can't seem to find any reference at all to this particular type of problem. I have a StreamReader object with a file, and I want to read over a certain number of lines in the file a certain number of times,…
Damon Swayn
  • 1,326
  • 2
  • 16
  • 36
3
votes
1 answer

StreamReader.ReadLine not working over TCP when using \r as line terminator

When I use only \r as a line terminator, the StreamReader.ReadLine() method doesn't work. It works if I use Environment.NewLine, \r\n or \ra ("a" being any character). Is this a bug? The same problem doesn't occurs when using MemoryStream instead of…
Jader Dias
  • 88,211
  • 155
  • 421
  • 625