streamreader

C# Stream.Read with timeout

徘徊边缘 提交于 2019-12-17 19:33:42
问题 I have this streamreader: Boolean read = false; while (wline!="exit") { while (!read || streamReader.Peek() >= 0) { read = true; Console.Write((char)streamReader.Read()); } wline = Console.ReadLine(); streamWriter.Write(wline+"\r\n"); streamWriter.Flush(); } How to set a timeout for Read() method? thanks 回答1: If this is System.IO.StreamReader , then set it on the BaseStream : streamReader.BaseStream.ReadTimeout = 2000; //milliseconds, so 2 seconds 回答2: You need to deal with the underlying

Httplistener and file upload

半腔热情 提交于 2019-12-17 16:08:09
问题 I am trying to retrieve an uploaded file from my webserver. As the client sends its files through a webform (random files), I need to parse the request to get the file out and to process it further on. Basically, the code goes as: HttpListenerContext context = listener.GetContext(); HttpListenerRequest request = context.Request; StreamReader r = new StreamReader(request.InputStream, System.Text.Encoding.Default); // this is the retrieved file from streamreader string file = null; while ((line

Do I need to explicitly close the StreamReader in C# when using it to load a file into a string variable?

孤街浪徒 提交于 2019-12-17 15:53:28
问题 Example: variable = new StreamReader( file ).ReadToEnd(); Is that acceptable? 回答1: No, this will not close the StreamReader. You need to close it. Using does this for you (and disposes it so it's GC'd sooner): using (StreamReader r = new StreamReader("file.txt")) { allFileText = r.ReadToEnd(); } Or alternatively in .Net 2 you can use the new File. static members, then you don't need to close anything: variable = File.ReadAllText("file.txt"); 回答2: You should always dispose of your resources. /

Converting file into Base64String and back again

♀尐吖头ヾ 提交于 2019-12-17 07:04:38
问题 The title says it all: I read in a tar.gz archive like so break the file into an array of bytes Convert those bytes into a Base64 string Convert that Base64 string back into an array of bytes Write those bytes back into a new tar.gz file I can confirm that both files are the same size (the below method returns true) but I can no longer extract the copy version. Am I missing something? Boolean MyMethod(){ using (StreamReader sr = new StreamReader("C:\...\file.tar.gz")) { String AsString = sr

How do I open an already opened file with a .net StreamReader?

半腔热情 提交于 2019-12-17 03:32:24
问题 I have some .csv files which I'm using as part of a test bench. I can open them and read them without any problems unless I've already got the file open in Excel in which case I get an IOException : System.IO.IOException : The process cannot access the file 'TestData.csv' because it is being used by another process. This is a snippet from the test bench: using (CsvReader csv = new CsvReader(new StreamReader(new FileStream(fullFilePath, FileMode.Open, FileAccess.Read)), false)) { // Process

How do I open an already opened file with a .net StreamReader?

冷暖自知 提交于 2019-12-17 03:32:08
问题 I have some .csv files which I'm using as part of a test bench. I can open them and read them without any problems unless I've already got the file open in Excel in which case I get an IOException : System.IO.IOException : The process cannot access the file 'TestData.csv' because it is being used by another process. This is a snippet from the test bench: using (CsvReader csv = new CsvReader(new StreamReader(new FileStream(fullFilePath, FileMode.Open, FileAccess.Read)), false)) { // Process

Closing a file without using using

让人想犯罪 __ 提交于 2019-12-14 03:49:56
问题 I have a class which reads data from one file stream and writes to another. I am concerned about closing the streams after the processing has finished in closeFiles(). How would you handle the possibility that the dispose of one stream may throw an exception stopping the dispose of the other stream from being called.? Should I be calling close and dispose on the streams or just one? What happens if I catch any errors from the stream disposes and then continue with moving and deleting of the

Where to use StreamReader.DiscardBufferedData()? [closed]

二次信任 提交于 2019-12-13 23:17:14
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I have the following code: StreamReader StreamReader = new StreamReader("File.txt"); string Line = ""; while((Line = StreamReader.ReadLine()) != null) { string Value = Line.Trim(); string Value2 = StreamReader.ReadLine(); int CursorValue = StreamReader.BaseStream.Position; if(Condition) {

Out of Memory Exception - Reading large text file for HttpWebRequest

岁酱吖の 提交于 2019-12-13 16:25:37
问题 I am trying to read 500 MB text file to send it contents through HttpWebRequest. According to my requirement, I cannot send the data in chunks. Code is as follows : using (StreamReader reader = new StreamReader(filename)) { postData = reader.ReadToEnd(); } byte[] byteArray = Encoding.UTF8.GetBytes(postData); request.ContentType = "text/plain"; request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream

using streamReader class to read text file with dynamic number of columns in c#

烈酒焚心 提交于 2019-12-13 15:53:45
问题 public class myRows { public decimal Col1 { get; set; } public decimal Col2 { get; set; } public decimal Col3 { get; set; } public decimal Col4 { get; set; } public decimal Col5 { get; set; } public decimal Col6 { get; set; } public string myDateTimeCol { get; set; } public myRows(string str) { var fields = str.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); Col1 = Convert.ToDecimal(fields[0]); Col2 = Convert.ToDecimal(fields[1]); Col3 = Convert.ToDecimal(fields[2]); Col4 =