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
196
votes
7 answers

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

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…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
190
votes
11 answers

Append lines to a file using a StreamWriter

I want to append lines to my file. I am using a StreamWriter: StreamWriter file2 = new StreamWriter(@"c:\file.txt"); file2.WriteLine(someString); file2.Close(); The output of my file should be several strings below each other, but I have only one…
Waypoint
  • 17,283
  • 39
  • 116
  • 170
112
votes
7 answers

MemoryStream - Cannot access a closed Stream

Hi why using (var sw = new StreamWriter(ms)) returns Cannot access a closed Stream exception. Memory Stream is on top of this code. using (var ms = new MemoryStream()) { using (var sw = new StreamWriter(ms)) { …
Arbejdsglæde
  • 13,670
  • 26
  • 78
  • 144
103
votes
6 answers

FileStream vs/differences StreamWriter?

What is difference 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?
HelloWorld1
  • 13,688
  • 28
  • 82
  • 145
96
votes
9 answers

Create File If File Does Not Exist

I need to get my code to read if file doesnt exist create else append. Right now it is reading if it does exist create and append. Here is the code: if (File.Exists(path)) { using (StreamWriter sw = File.CreateText(path)) { Would I do…
shan
  • 1,311
  • 4
  • 13
  • 17
49
votes
3 answers

NodeJS write binary buffer into a file

I can't rewrite a file that I am getting from a binary buffer, I have checked with the original file and all bytes are the same. This is the file create from NodeJS: # hd test.txt | head 00000000 47 49 46 38 39 61 32 00 32 00 f7 00 00 96 8c 73 …
max246
  • 568
  • 2
  • 5
  • 11
47
votes
3 answers

Writing to MemoryStream with StreamWriter returns empty

I am not sure what I am doing wrong, have seen a lot of examples, but can't seem to get this working. public static Stream Foo() { var memStream = new MemoryStream(); var streamWriter = new StreamWriter(memStream); for (int i = 0; i <…
jsmith
  • 7,198
  • 6
  • 42
  • 59
47
votes
4 answers

Writing file to web server - ASP.NET

I simply want to write the contents of a TextBox control to a file in the root of the web server directory... how do I specify it? Bear in mind, I'm testing this locally... it keeps writing the file to my program files\visual studio\Common\IDE…
Woody
  • 1,159
  • 3
  • 15
  • 25
41
votes
1 answer

What is the default buffer size for StreamWriter

For the public StreamWriter(Stream stream) constructor, MSDN says Initializes a new instance of the StreamWriter class for the specified stream by using UTF-8 encoding and the default buffer size. I want to use one of the other constructor…
rory.ap
  • 34,009
  • 10
  • 83
  • 174
41
votes
5 answers

Redirect .NET StreamWriter output to a String variable

I'd like to know if it is possible to redirect StreamWriter output to a variable Something like String^ myString; StreamWriter sw = gcnew StreamWriter([somehow specify myString]) sw->WriteLine("Foo"); then myString will contain Foo. The reason I…
Eric
  • 19,525
  • 19
  • 84
  • 147
40
votes
6 answers

how to read special character like é, â and others in C#

I can't read those special characters I tried like this 1st way # string xmlFile = File.ReadAllText(fileName); 2nd way # FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); StreamReader r = new StreamReader(fs); string s =…
kevin
  • 13,559
  • 30
  • 79
  • 104
33
votes
1 answer

Does a Stream get Disposed when returning a File from an Action?

I'm writing a string to a MemoryStream I need to return the stream to the Controller Action so I can send it off as a file for download. Normally, I wrap the Stream in a using statement, but, in this case, I need to return it. Does it still get…
John-Luke Laue
  • 3,736
  • 3
  • 32
  • 60
31
votes
2 answers

Append text using StreamWriter

This is probably a pretty simple question. In C# I'm trying to write a simple method, called my "DebugWrite" method, to write out any exceptions caught within my program to a text file stored locally. My current code only writes a new file every…
keynesiancross
  • 3,441
  • 15
  • 47
  • 87
31
votes
5 answers

Forcing StreamWriter to change Encoding

I am trying to save a file using DialogResult and StringBuilder. After making the text, I am calling the following code to save the file: if (dr == DialogResult.OK) { StreamWriter sw = new StreamWriter(saveFileDialog1.FileName); …
Dumbo
  • 13,555
  • 54
  • 184
  • 288
29
votes
2 answers

Do I need to do StreamWriter.flush()?

Suppose this C# code: using (MemoryStream stream = new MemoryStream()) { StreamWriter normalWriter = new StreamWriter(stream); BinaryWriter binaryWriter = new BinaryWriter(stream); foreach(...) { binaryWriter.Write(number); …
Nefzen
  • 7,819
  • 14
  • 36
  • 34
1
2 3
99 100