streamreader

.NET C# - Random access in text files - no easy way?

Hi all, I've got a text file that contains several 'records' inside of it. Each record contains a name and a collection of numbers as data. I'm trying to build a class that will read through the file, present only the names of all the records, and then allow the user to select which record data he/she wants. The first time I go thro...

StreamReader problem - Unknown file encoding (western iso 88591)

When reading data from the Input file I noticed that the ¥ symbom was not being read by the StreamReader. Mozilla Firefox showed the input file type as Western (ISO-8859-1). After playing around with the encoding parameters I found it worked successfully for the following values: System.Text.Encoding.GetEncoding(1252) // (western iso 8...

How to limit the number of characters read by StreamReader.ReadLine() in .NET?

I am writing a web server application in C# and using StreamReader class to read from an underlying NetworkStream: NetworkStream ns = new NetworkStream(clientSocket); StreamReader sr = new StreamReader(ns); String request = sr.ReadLine(); This code is prone to DoS attacks because if the attacker never disconnects we will never fini...

Why does my loop use 100% CPU and never end?

I have this method: private delegate void watcherReader(StreamReader sr); private void watchProc(StreamReader sr) { while (true) { string line = sr.ReadLine(); while (line != null) { if (stop) { return; } //Console.WriteLine(line)...

How do I specify EOF in SSIS VBScript?

What is the syntax for reading until the end of file in SSIS VBScript? Dim readFile As FileInfo = New FileInfo(logHourlyName) If readFile.Exists() Then Dim textStream As StreamReader = readFile.OpenText() Dim strLine As String Do While Not EOF <--- what goes here? curLine = textStream.ReadLine() Loop textStream....

How can I determine the content type of a file in .NET?

I'm given a filename and I have to be able to read it from disk and send its contents over a network. I need to be able to determine whether the file is text or binary so I know whether to use a StreamReader or BinaryReader. Another reason why I need to know the content type is because if it is binary, I have to MIME encode the data be...

.NET StreamReader Won't Close

I'm using a RTF file as a template for an ASP.NET web application. My VB.NET code reads the file using a StreamReader, replaces some strings, and creates a new Word document with data from the database. My code closes and disposes of the StreamReader. However, when I attempt to upload a revised RTF file to the web server I get an error, ...

Efficient way to encode CDATA elements

Ok, I'm reading data from a stream using a StreamReader. The data inside the stream is not xml, it could be anything. Based on the input StreamReader I'm writing to an output stream using an XmlTextWriter. Basically, when all is said and done, the output stream contains data from the input stream wrapped in a element contained in a p...

How to Display the Contents from StreamReader directly on Notepad?

Is there a way to display the contents from memory directly in a Notepad window? ...

C#: How do you specifiy where to start reading in a file when using StreamReader?

C#: How do you specifiy where to start reading in a file when using StreamReader? I have created a streamreader object, along with a file stream object. After both objects are created, how would I go upon controlling where I want the StreamReader to start reading from a file? Let's say the file's contents are as follows, // song list...

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 that you can rely on StreamReader.ReadBlock not returning until either it's read as mu...

Lengthy lines of code vs readability

This is perfectly fine C# code and works fine provided correct URL. But the everything is just done at one line by reducing the readability of the code. Here is the code : return new StreamReader(WebRequest.Create(urlName).GetResponse().GetResponseStream()).ReadToEnd(); I am just wondering what are the opinions of fellow de...

File encoding when reading a file with StreamReader

I am now having an issue where Celsius symbol gets read as C instead of °C. Looks like the encoding the culprit. I tried to do this: using (StreamReader sr = new StreamReader(this._inFilePath,System.Text.Encoding.Unicode ,true)) instead of using (StreamReader sr = new StreamReader(this._inFilePath)) but I a...

Converting file encoding after reading StreamReader in Compact Framework

I read the text file line by line, so far so good. I just use this: using (StreamReader sr = new StreamReader(this._inFilePath)) { string line; int index = 0; // Read and display lines from the file until the end of // the file is reached: ...

C# StreamReader.ReadLine() - Need to pick up line terminators

I wrote a C# program to read an Excel .xls/.xlsx file and output to CSV and Unicode text. I wrote a separate program to remove blank records. This is accomplished by reading each line with StreamReader.ReadLine(), and then going character by character through the string and not writing the line to output if it contains all commas (for th...

Is C# 3.0 ElementAt broken for iterator functions?

Can anyone tell me why the following code doesn't work the way I expect? I'm trying to write an IEnumberable wrapper around a StreamReader, but when I use ElementAt on it, it reads sequential characters from the stream regardless of the index I pass to ElementAt. The file "test.txt" contains "abcdefghijklmnopqrstuvwxyz". I would expec...

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", "somefile.bak"); //can't move, get exception that I the file is open I thought that because the using...

How to know position(linenumber) of a streamreader in a textfile?

an example (that might not be real life, but to make my point) : public void StreamInfo(StreamReader p) { string info = string.Format( "The supplied streamreaer read : {0}\n at line {1}", p.ReadLine(), p.GetLinePosition()-1); } GetLinePosition here is an imaginary extension method of streamr...

Reading a line from a streamreader without consuming?

Is there a way to read ahead one line to test if the next line contains specific tag data? I'm dealing with a format that has a start tag but no end tag. I would like to read a line add it to a structure then test the line below to make sure it not a new "node" and if it isn't keep adding if it is close off that struct and make a new o...

Why does the .NET Framework StreamReader / Writer default to UTF8 encoding?

I'm just looking at the constructors for StreamReader / Writer and I note it uses UTF8 as default. Anyone know why this is? I would have presumed it would have been a safer bet to default to Unicode. ...