C# Stream.Read with timeout
If this is System.IO.StreamReader, then set it on the BaseStream: streamReader.BaseStream.ReadTimeout = 2000; //milliseconds, so 2 seconds
If this is System.IO.StreamReader, then set it on the BaseStream: streamReader.BaseStream.ReadTimeout = 2000; //milliseconds, so 2 seconds
You can do this one of three ways: 1) Write your own StreamReader. Here’s a good place to start: How to know position(linenumber) of a streamreader in a textfile? 2) The StreamReader class has two very important, but private variables called charPos and charLen that are needed in locating the actual “read” position and not … Read more
I came across this post while looking for a solution to a similar problem where I needed to seek the StreamReader to particular lines. I ended up creating two extension methods to get and set the position on a StreamReader. It doesn’t actually provide a line number count, but in practice, I just grab the … Read more
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
I think you are making things harder on yourself than necessary by doing this with an HttpListener rather than using the built in facilities of ASP.Net. But if you must do it this way here is some sample code. Note: 1) I’m assuming you’re using enctype=”multipart/form-data” on your <form>. 2) This code is designed to … Read more
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
With regular files there’s no way around it – you must read the text that follows the line you wish to append after, overwrite the file, and then append the original trailing text. Think of files on disk as arrays – if you want to insert some items into the middle of an array, you … Read more
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
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();
If you want for some reason to convert your file to base-64 string. Like if you want to pass it via internet, etc… you can do this Byte[] bytes = File.ReadAllBytes(“path”); String file = Convert.ToBase64String(bytes); And correspondingly, read back to file: Byte[] bytes = Convert.FromBase64String(b64Str); File.WriteAllBytes(path, bytes);