Questions tagged [streamwriter]

StreamWriter is designed for character output in a particular encoding, whereas classes derived from Stream are designed for byte input and output.

In .Net StreamWriter (under System.IO) is designed for character output in a particular encoding, whereas classes derived from Stream are designed for byte input and output.

StreamWriter defaults to using an instance of UTF8Encoding unless specified otherwise. This instance of UTF8Encoding is constructed without a byte order mark (BOM), so its GetPreamble method returns an empty byte array. The default UTF-8 encoding for this constructor throws an exception on invalid bytes. This behavior is different from the behavior provided by the encoding object in the Encoding.UTF8 property. To specify a BOM and determine whether an exception is thrown on invalid bytes, use a constructor that accepts an encoding object as a parameter, such as StreamWriter(String, Boolean, Encoding) or StreamWriter.

By default, a StreamWriter is not thread safe
See official documentation from Microsoft.

1524 questions
7
votes
3 answers

C# - FileStream: both lock a file and at the same time be able to read it without truncating it and write it with truncating it

I suppose my title isn't that clear. I'll try to explain: I can write and read a file using a FileStream FileStream fs = new FileStream("C:\\Users\\Public\\text.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); private void…
pikachu
  • 1,031
  • 2
  • 11
  • 20
7
votes
1 answer

Node.js writable.write return false?

I want to upload (stream) control the writing process. But the writing process always returns false. Large files upload process stops. The output of the code is as follows; > Node app.js > False > False > False > False What am I doing wrong? My…
Wise Colt
  • 131
  • 2
  • 7
7
votes
5 answers

How to create copy of file using StreamReader and StreamWriter

I need to use StreamReader to read a .txt file on a console application, then create a new file or backup with a different name but same content. The problem is i cant figure out how to use the content from the first file to place into the new one.…
claraichu
  • 71
  • 1
  • 1
  • 2
7
votes
4 answers

StreamWriter add an extra \r in the end of the line

I created a class with the responsibility to generate a text file where each line represents the information of an object of 'MyDataClass' class. Below is a simplification of my code: public class Generator { private readonly Stream _stream; …
Jonny Piazzi
  • 3,684
  • 4
  • 34
  • 81
7
votes
1 answer

StreamWriter limit in C# in text file

I have an array list which contains 100 lines. When i try to export it into a text file (txt), the output is only 84 lines and it stops in the middle of the 84th line. When I looked at the file size it showed exactly sharp 4.00KB as if there is some…
Snooker Ball
  • 85
  • 1
  • 2
  • 10
7
votes
4 answers

How to send keys instead of characters to a process?

System.Diagnostics.Process exposes a StreamWriter named StandardInput, which accepts only characters as far as I know. But I need to send keystrokes as well, and some keystrokes don't map well to characters. What should I do?
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
7
votes
4 answers

Why does my C# program only write 251 rows to a file?

I've got a list of 369 different names and I want to print these names into a csv file. All's going well until I take a look at the outputted csv file and it only has 251 rows. I've tried outputting to a .txt instead, and still it only outputs 251…
Jimmy T
  • 447
  • 6
  • 14
7
votes
4 answers

Optimizing Listview in C# for large files

I have a C# program that is pulling in a .csv file that is approx. 42,000 lines long. All data in the file is stored as follows: Zipcode,City,State I am pulling all the information into three different columns in a listview. Currently this data…
Mark P.
  • 1,827
  • 16
  • 37
7
votes
3 answers

Power Loss after StreamWriter.Close() produces blank file, why?

Ok, so to explain; I am developing for a system that can suffer a power failure at any point in time, one point that I am testing is directly after I have written a file out using a StreamWriter. The code below: // Write the updated file back out to…
Siyfion
  • 416
  • 1
  • 6
  • 17
7
votes
4 answers

How I get the best performance out of .NET's StreamWriter in C#?

What is the recommended approach to get the best performance when we need to create text files bigger than 10 MB? There are multiple sections in the code that need to write stuff to a single file. This means a lot of text lines. Option #1 (This…
Carlos Castillo
7
votes
7 answers

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

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?
StackTrace
  • 9,190
  • 36
  • 114
  • 202
6
votes
6 answers

C# add text to text file without rewriting it?

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…
Oztaco
  • 3,399
  • 11
  • 45
  • 84
6
votes
3 answers

How to Write to a file using StreamWriter?

in my Wpf app I'm using a class Person (that is a base class), and that contains a virtual method SaveData(), and other class Client that inherits from Person. How to override method SaveData() and keeping data from base? Class Person public virtual…
Wesley Heron
  • 413
  • 2
  • 12
  • 27
6
votes
3 answers

"Stream not Writable" error when using StreamWriter in loop

I'm trying to write a tool in C# to help QA some network issues, and am running into a problem. The program is supposed to send a query in JSON format to the server every second. Currently, it works once, but on the second attempt to send the…
MacHume
  • 73
  • 1
  • 3
6
votes
2 answers

StreamWriter Not working in C#

This piece of code worked perfectly in VS 2010. Now that I have VS 2013 it no longer writes to the file. It doesn't error or anything. (I get an alert in Notepad++ stating that the file has been updated, but there is nothing written.) It all looks…
tvoytko
  • 93
  • 1
  • 6