HTTPWebResponse + StreamReader Very Slow

HttpWebRequest may be taking a while to detect your proxy settings. Try adding this to your application config: <system.net> <defaultProxy enabled=”false”> <proxy/> <bypasslist/> <module/> </defaultProxy> </system.net> You might also see a slight performance gain from buffering your reads to reduce the number of calls made to the underlying operating system socket: using (BufferedStream buffer = … Read more

c# – StreamReader and seeking

I realize this is really belated, but I just stumbled onto this incredible flaw in StreamReader myself; the fact that you can’t reliably seek when using StreamReader. Personally, my specific need is to have the ability to read characters, but then “back up” if a certain condition is met; it’s a side effect of one … Read more

An elegant way to consume (all bytes of a) BinaryReader?

Original Answer (Read Update Below!) Simply do: byte[] allData = read1.ReadBytes(int.MaxValue); The documentation says that it will read all bytes until the end of the stream is reached. Update Although this seems elegant, and the documentation seems to indicate that this would work, the actual implementation (checked in .NET 2, 3.5, and 4) allocates a … Read more

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

This is a slight modification from: http://msdn.microsoft.com/en-us/library/aa287535%28VS.71%29.aspx int counter = 0; string line; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader(“c:\\test.txt”); while((line = file.ReadLine()) != null) { if ( line.Contains(“word”) ) { Console.WriteLine (counter.ToString() + “: ” + line); } counter++; } file.Close();