streamwriter

How to handle concurrent file access with a filestream/streamwriter?

情到浓时终转凉″ 提交于 2019-12-19 08:57:02
问题 I am writing an audit file that is writing the username, time, and the old/changed values of several variables in the application for each user when they use my application. It is using a FileStream and StreamWriter to access the audit file. All audits for each user will be written to the same file. The issue is that when two users are updating this audit file at the same time, the "old value" of each of the variable is mixing up between the users. Why is this and how can you solve the

StreamWriter: (Write+Flush)Async seem much slower then synchronous variants

烈酒焚心 提交于 2019-12-19 04:16:07
问题 I was trying to find an IO bottleneck in my class and surprisingly noticed that sw.Write("m"); sw.Flush() can be 20000 faster then await sw.WriteAsync("m"); Await sw.FlushAsync(); when writing 1000000 messages to a file. Does, by any chance, anyone know why? My bet is StreamWriter 's constructor taking a String does not parametrize a stream for async usage. The code below can be launched from C# interactive. Yes, it's not the best place to measure the speed but it will show the matter at hand

c# HttpWebRequest POST'ing failing

限于喜欢 提交于 2019-12-18 15:55:34
问题 So i'm trying to POST something to a webserver. System.Net.HttpWebRequest EventReq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("url"); System.String Content = "id=" + Id; EventReq.ContentLength = System.Text.Encoding.UTF8.GetByteCount(Content); EventReq.Method = "POST"; EventReq.ContentType = "application/x-www-form-urlencoded"; System.IO.StreamWriter sw = new System.IO.StreamWriter(EventReq.GetRequestStream(), System.Text.Encoding.UTF8); sw.Write(Content); sw.Flush(); sw

FileStream vs/differences StreamWriter?

假装没事ソ 提交于 2019-12-18 10:01:28
问题 Question: What is different between FileStream and StreamWriter in .Net ? What context are you supposed to use it? What is their advantage and disadvantage? Is it possible to combine these two into one? 回答1: What is different between FileStream and StreamWriter in dotnet? A FileStream is a Stream . Like all Streams it only deals with byte[] data. A StreamWriter : TextWriter , is a Stream-decorator. A TextWriter encodes Text data like string or char to byte[] and then writes it to the linked

StringBuilder.ToString() throws OutOfMemoryException

别来无恙 提交于 2019-12-18 05:44:14
问题 I have a created a StringBuilder of length "132370292", when I try to get the string using the ToString() method it throws OutOfMemoryException . StringBuilder SB = new StringBuilder(); for(int i =0; i<=5000; i++) { SB.Append("Some Junk Data for testing. My Actual Data is created from different sources by Appending to the String Builder."); } try { string str = SB.ToString(); // Throws OOM mostly Console.WriteLine("String Created Successfully"); } catch(OutOfMemoryException ex) { StreamWriter

What consumes less resources and is faster File.AppendText or File.WriteAllText storing first in StringBuilder?

点点圈 提交于 2019-12-18 03:40:17
问题 I have to write thousands of dynamically generated lines to a text file. I have two choices, Which consumes less resources and is faster than the other? A. Using StringBuilder and File.WriteAllText StringBuilder sb = new StringBuilder(); foreach(Data dataItem in Datas) { sb.AppendLine( String.Format( "{0}, {1}-{2}", dataItem.Property1, dataItem.Property2, dataItem.Property3)); } File.WriteAllText("C:\\example.txt", sb.ToString(), new UTF8Encoding(false)); B. Using File.AppendText using

Exporting datagridview to csv file

不想你离开。 提交于 2019-12-17 07:35:44
问题 I'm working on a application which will export my DataGridView called scannerDataGridView to a csv file. Found some example code to do this, but can't get it working. Btw my datagrid isn't databound to a source. When i try to use the Streamwriter to only write the column headers everything goes well, but when i try to export the whole datagrid including data i get an exeption trhown. System.NullReferenceException: Object reference not set to an instance of an object. at Scanmonitor.Form1

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

C# add text to text file without rewriting it?

前提是你 提交于 2019-12-13 14:14:18
问题 Let's say I have the following code: StreamWriter sw = new StreamWriter(File.OpenWrite(Path)); sw.Write("Some stuff here"); sw.Dispose(); This code replaces the contents of the file with "Some stuff here," however I would like to add text to the file rather than replacing the text. How would I do this? 回答1: You need to tell the StreamWriter you want to append: var sw = new StreamWriter(path, true); File.OpenWrite doesn't support appending. 回答2: You could use the File.AppendAllText method and

StreamWriter/StreamReader File In Use By Another Process

时光总嘲笑我的痴心妄想 提交于 2019-12-13 13:21:50
问题 I have a StreamWriter object to write to a log file, it is initialised as follows: StreamWriter streamWriter = File.AppendText(GetLogFilePath()); streamWriter.AutoFlush = true; Later in my code, I need to close the log file, and then read in part of the logged out contents. I have the following code: streamWriter.Close(); streamWriter = null; StreamReader logFileStream = File.OpenText(GetLogFilePath()); This is throwing an exception: The process cannot access the file because it is being used