streamreader

StreamReader and seeking

。_饼干妹妹 提交于 2019-11-26 08:26:48
问题 can you use streamreader to read a normal textfile and then in the middle of reading close the streamreader after saving the current position and then open streamreader again and start reading from that poision ? if not what else can i use to accomplish the same case without locking the file ? something like this: var fs = File.Open(@\"C:\\testfile.txt\", FileMode.Open, FileAccess.Read); var sr = new StreamReader(fs); Debug.WriteLine(sr.ReadLine());//Prints:firstline var pos = fs.Position;

How to Find And Replace Text In A File With C#

元气小坏坏 提交于 2019-11-26 04:06:56
问题 My code so far StreamReader reading = File.OpenText(\"test.txt\"); string str; while ((str = reading.ReadLine())!=null) { if (str.Contains(\"some text\")) { StreamWriter write = new StreamWriter(\"test.txt\"); } } I know how to find the text, but I have no idea on how to replace the text in the file with my own. 回答1: Read all file content. Make a replacement with String.Replace . Write content back to file. string text = File.ReadAllText("test.txt"); text = text.Replace("some text", "new

simultaneous read-write a file in C#

為{幸葍}努か 提交于 2019-11-26 03:59:16
问题 I have a file containing data that I\'d like to monitor changes to, as well as add changes of my own. Think like \"Tail -f foo.txt\". Based on this thread, it looks like I should just create a filestream, and pass it both to a writer and reader. However, when the reader reaches the end of the original file, it fails to see updates I write myself. I know it seems like a weird situation... its more an experiment to see if it can be done. Here is the example case I tried: foo.txt: a b c d e f

Should I call Close() or Dispose() for stream objects?

為{幸葍}努か 提交于 2019-11-26 03:34:12
问题 Classes such as Stream , StreamReader , StreamWriter etc implements IDisposable interface. That means, we can call Dispose() method on objects of these classes. They\'ve also defined a public method called Close() . Now that confuses me, as to what should I call once I\'m done with objects? What if I call both? My current code is this: using (Stream responseStream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(responseStream)) { using (StreamWriter writer =

Reading large text files with streams in C#

半腔热情 提交于 2019-11-26 01:24:29
问题 I\'ve got the lovely task of working out how to handle large files being loaded into our application\'s script editor (it\'s like VBA for our internal product for quick macros). Most files are about 300-400 KB which is fine loading. But when they go beyond 100 MB the process has a hard time (as you\'d expect). What happens is that the file is read and shoved into a RichTextBox which is then navigated - don\'t worry too much about this part. The developer who wrote the initial code is simply

Does disposing streamreader close the stream?

 ̄綄美尐妖づ 提交于 2019-11-26 01:07:13
问题 I am sending a stream to methods to write on, and in those methods I am using a binary reader/wrtier. When the reader/writer gets disposed, either by using or just when it is not referenced, is the stream closed as well?? I would send a BinaryReader/Writer, but I am using a StreamReader too (maybe I should go around that. I am only using that for GetLine and ReadLine). This is quite troublesome if it closes the stream each time a writer/reader gets closed. 回答1: Yes, StreamReader ,

Does disposing streamreader close the stream?

南笙酒味 提交于 2019-11-26 01:06:42
I am sending a stream to methods to write on, and in those methods I am using a binary reader/wrtier. When the reader/writer gets disposed, either by using or just when it is not referenced, is the stream closed as well?? I would send a BinaryReader/Writer, but I am using a StreamReader too (maybe I should go around that. I am only using that for GetLine and ReadLine). This is quite troublesome if it closes the stream each time a writer/reader gets closed. Yes, StreamReader , StreamWriter , BinaryReader and BinaryWriter all close/dispose their underlying streams when you call Dispose on them.

Memory Leak using StreamReader and XmlSerializer

只谈情不闲聊 提交于 2019-11-26 00:08:57
问题 I\'ve been googling for the past few hours and trying different things but can\'t seem to the bottom of this.... When I run this code, the memory usage continuously grows. while (true) { try { foreach (string sym in stringlist) { StreamReader r = new StreamReader(@\"C:\\Program Files\\\" + sym + \".xml\"); XmlSerializer xml = new XmlSerializer(typeof(XMLObj), new XmlRootAttribute(\"rootNode\")); XMLObj obj = (XMLObj)xml.Deserialize(r); obj.Dispose(); r.Dispose(); r.Close(); } } catch

How to read embedded resource text file

橙三吉。 提交于 2019-11-25 21:45:54
问题 How do I read an embedded resource (text file) using StreamReader and return it as a string? My current script uses a Windows form and textbox that allows the user to find and replace text in a text file that is not embedded. private void button1_Click(object sender, EventArgs e) { StringCollection strValuesToSearch = new StringCollection(); strValuesToSearch.Add(\"Apple\"); string stringToReplace; stringToReplace = textBox1.Text; StreamReader FileReader = new StreamReader(@\"C:\\MyFile.txt\"