Skip to main content

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.

streamwriter
203 votes
7 answers
191k views

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 ...
Sarfaraz Nawaz's user avatar
194 votes
11 answers
461k views

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 ...
Waypoint's user avatar
  • 17.6k
118 votes
7 answers
260k views

MemoryStream - Cannot access a closed Stream

Why using (var sw = new StreamWriter(ms)) returns Cannot access a closed Stream exception while the MemoryStream is on top of this code? using (var ms = new MemoryStream()) { using (var sw = new ...
Arbejdsglæde's user avatar
106 votes
6 answers
78k views

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's user avatar
98 votes
9 answers
304k views

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 ...
shan's user avatar
  • 1,351
51 votes
3 answers
154k views

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 ...
max246's user avatar
  • 608
48 votes
3 answers
61k views

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 ...
jsmith's user avatar
  • 7,258
47 votes
4 answers
154k views

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 ...
Woody's user avatar
  • 1,179
44 votes
4 answers
51k views

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("...
Eric's user avatar
  • 19.8k
42 votes
1 answer
28k views

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 ...
rory.ap's user avatar
  • 34.8k
41 votes
6 answers
76k views

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); ...
kevin's user avatar
  • 13.8k
34 votes
1 answer
14k views

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

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 ...
John-Luke Laue's user avatar
31 votes
2 answers
102k views

Append text using StreamWriter [duplicate]

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....
keynesiancross's user avatar
31 votes
5 answers
104k views

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 ...
Dumbo's user avatar
  • 13.9k
29 votes
3 answers
121k views

How To Overwrite A File If It Already Exists?

I'm making a music player. It has 2 forms; one is the main area where you play music. The second form has a CheckedListBox where you select the mp3s you want. When I click a button, it saves the ...
Manny's user avatar
  • 345

15 30 50 per page
1
2 3 4 5
102