streamwriter

What happens if StreamReader or StreamWriter are not closed?

▼魔方 西西 提交于 2020-01-11 09:29:11
问题 I'm working on an assignment for a professor that is strict about LOC. For this reason I'd like to do the following: (new StreamWriter(saveFileDialog.FileName)).Write(textBox.Text); instead of StreamWriter sw = new StreamWriter(saveFileDialog.FileName); sw.Write(textBox.Text); sw.Close(); In the first example I don't close the stream. Is this ok? Will it cause any security or memory problems? 回答1: You may not get any output, or incomplete output. Closing the writer also flushes it. Rather

StreamWriter won't write to log file

血红的双手。 提交于 2020-01-06 08:38:17
问题 This code will create "output.txt" in C:\temp if it doesn't already exist. However, log.WriteLine(); doesn't work for me. When I open the file, I don't see it. Why is this? private static string LogFile = @"C:\Temp\output.txt"; private StreamWriter log; if (!File.Exists(@"" + LogFile)) { log = new StreamWriter(@"" + LogFile); } else { log = File.AppendText(@"" + LogFile); } log.WriteLine("["+DateTime.Now + "]: "); 回答1: You need to close the StreamWriter . It's best to use a using block for

How to close a stream properly?

自作多情 提交于 2020-01-04 10:59:08
问题 I have been assigned a task to write a program that will: Open a file. Read the content. Replace a specific word with another word. Save the changes to the file. I know for sure that my code can open, read and replace words. The problem occurs when i add the "Save the changes to the file" - part. Here is the code: open System.IO //Getting the filename, needle and replace-word. System.Console.WriteLine "What is the name of the file?" let filename = string (System.Console.ReadLine ()) System

VB.Net Replacing Specific Values in a Large Text File

偶尔善良 提交于 2020-01-04 05:28:49
问题 I have some large csv files (1.5gb each) where I need to replace specific values. The method I'm currently using is terribly slow and I'm fairly certain that there should be a way to speed this up but I'm just not experienced enough to know what I should be doing. This is my first post and I tried searching through to find something relevant but didn't come across anything. Any help would be appreciated. My other thought would be to break the file into chunks so that I can read the entire

StreamWriter Writing NUL characters at the end of the file when system shutdown abruptly

余生长醉 提交于 2020-01-04 04:19:14
问题 I am writing some test application to write some text into a text file using StreamWriter. While executing the WriteLine method system shutdown abruptly. After rebooting the machine have observed that there are many NUL characters at the end of file. Have searched many sites including MSDN I didn't found the solution for this problem. Could any one help me to resolve this problem. This can be easily reproducible if we do the following steps: Create WindowsApplication and place a button

How can I remove the oldest lines in a file when using a FileStream and StreamWriter?

梦想与她 提交于 2019-12-31 07:31:41
问题 Based on Prakash's answer here, I thought I'd try something like this to remove the oldest lines in a file prior to adding a new line to it: private ExceptionLoggingService() { _fileStream = File.OpenWrite(GetExecutionFolder() + "\\Application.log"); _streamWriter = new StreamWriter(_fileStream); } public void WriteLog(string message) { const int MAX_LINES_DESIRED = 1000; StringBuilder formattedMessage = new StringBuilder(); formattedMessage.AppendLine("Date: " + DateTime.Now.ToString());

unable to overwrite file using streamwriter despite append= false, without closing file

放肆的年华 提交于 2019-12-31 05:36:08
问题 using C# VS 2010, windows forms. My goal is to open and close the file only once and "overwrite" it multiple times. I never want to append. The reason for opening and closing the file once is I want the write operation to be fastest. I am passing append = false in streamwriter constructor but it still appends and not overwrite. private void testSpeed() { StreamWriter sw1 = new StreamWriter(@"d:\logfolder\overwrite.txt", false); sw1.AutoFlush = true; for (int i = 0; i < 5000; i++) { sw1.Write

What is the fastest way to export dataGridView rows to Excel or into an SQL Server database

落爺英雄遲暮 提交于 2019-12-30 18:40:08
问题 What is the fastest way to export DataGridView rows in the range of 460328 - 800328 to Excel or into an SQL Server database table with out using Microsoft office interop as interop is quite slow and heavy on system resources? 回答1: For exporting to Excel, if you aren't using the XML based 2007 or 2010, Interop is pretty much the only way to go. It's not as bad as it's reputation though. I'll list a few solutions. 1 To Excel First add a Microsoft.Office.Interop.Excel component reference to your

C# Unit Test a StreamWriter parameter

丶灬走出姿态 提交于 2019-12-30 05:58:09
问题 I have a bunch of classes that all implement an Interface and one of the parameters is a StreamWriter. I need to check the contents of the StreamWriter. I am trying to find a way to avoid writing text files on the test server and opening them to check the contents. Is there is a way to quickly convert the StreamWriter contents/stream to a StringBuilder variable? 回答1: You cannot check the StreamWriter . You could check the underlying stream it is writing to. So you could use a MemoryStream in

Writing List<String> contents to text file after deleting string

妖精的绣舞 提交于 2019-12-26 07:19:35
问题 I'm trying to get the contents of a Text File, delete a line of string, and re-write back to the Text File, deleting the line of string. I'm using StreamReader to get the text, importing into a List, removing the string, then rewriting using StreamWriter. My problems arises somewhere around the removing or writing of the string. Instead of writing back the existing, non deleted contents to the text file, all the text is replaced with : System.Collections.Generic.List`1[System.String] My code