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
11
votes
6 answers

Using StreamWriter to implement a rolling log, and deleting from top

My C# winforms 4.0 application has been using a thread-safe streamwriter to do internal, debug logging information. When my app opens, it deletes the file, and recreates it. When the app closes, it saves the file. What I'd like to do is modify my…
greggorob64
  • 2,487
  • 2
  • 27
  • 55
11
votes
5 answers

How to make a "Read only" file?

I'm using the C# StreamWritier class. Questions: How can I make a file read-only, so that no one can delete or write to it? How can I make a hidden file? I'm creating the file like so: private void button1_Click(object sender, EventArgs e) …
modest and cute girl
  • 785
  • 4
  • 13
  • 24
10
votes
2 answers

Can I write a file to a folder on a server machine from a Web API app running on it?

I have this code in my Web API app to write to a CSV file: private void SaveToCSV(InventoryItem invItem, string dbContext) { string csvHeader =…
10
votes
1 answer

How to detect a Socket Disconnect in C#

I'm working on a client/server relationship that is meant to push data back and forth for an indeterminate amount of time. The problem I'm attempting to overcome is on the client side, being that I cannot manage to find a way to detect a…
DigitalJedi805
  • 1,486
  • 4
  • 16
  • 41
10
votes
5 answers

how to use StreamWriter class properly?

I am using StreamWriter class for file operations, are there any problems in this code that I am not seeing? Such as do I need to put it into a try catch finally block? StreamWriter sr = new…
vettori
  • 731
  • 8
  • 17
  • 26
9
votes
2 answers

Reading and writing very large text files in C#

I have a very large file, almost 2GB in size. I am trying to write a process to read the file in and write it out without the first row. I pretty much have been only able to read and write one line at a time which takes forever. I can open it,…
Cass
  • 537
  • 1
  • 7
  • 24
9
votes
3 answers

Poll application: StreamWriter not writing to file

I'm writing a program for school so I don't outright want someone to fix it for me, but if someone could point out where they see the error, that would be really helpful! :D The program is supposed to take in a series of numbers in a text box, with…
9
votes
3 answers

Save file - xmlSerializer

I'm creating a method to serialize a file using this code: public void Save(Object file, Type type, String path) { // Create a new Serializer XmlSerializer serializer = new XmlSerializer(typeof(type)); // Create a new StreamWriter …
Erik
  • 2,500
  • 6
  • 28
  • 49
9
votes
1 answer

Is there any reason that default buffer sizes are 4096?

I often see 4096 used as a default buffer size all over the place. Is there any reason why 4096 was selected as opposed to another value?
myermian
  • 31,823
  • 24
  • 123
  • 215
9
votes
3 answers

c# HttpWebRequest POST'ing failing

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 =…
KJ Tsanaktsidis
  • 1,255
  • 1
  • 17
  • 28
9
votes
2 answers

Apache POI: SXSSFWorkbook.dispose() does not exist

I'm using apache's POI API to write XLSX files. Since I need to write big files, I'm using the Streaming API (SXSSF). To do this, I'm following this guide. Note that by the end of the example there's a call to wb.dispose This wb instance refers to…
Miguel Ribeiro
  • 8,057
  • 20
  • 51
  • 74
8
votes
2 answers

FileStream's FileMode.OpenOrCreate overwrites file

Documentation says FileMode.OpenOrCreate "specifies that the operating system should open a file if it exists; otherwise, a new file should be created", which sounds like it will open the file and write to it. Instead, the file seems to be…
Sinjai
  • 1,085
  • 1
  • 15
  • 34
8
votes
5 answers

Writing many variables into textfile

I've this piece of code: using (StreamWriter writer = new StreamWriter("C:\\Users\\HP8200\\Desktop\\teste.txt")) { string numcont = _transaction.PartyFederalTaxID; double numenc = _transaction.BillToPartyID; double numfatura…
joao costa
  • 141
  • 10
8
votes
2 answers

Writing to file using StreamWriter much slower than file copy over slow network

I have a program that attempts to write quite a large amount of text to a file on a remote server overseas, which has a slow network connection. Using the following code, where outputFileContent is a StringBuilder: using (var outfile = new…
Stephen Holt
  • 2,360
  • 4
  • 26
  • 34
8
votes
2 answers

Implementing HttpWebRequest Async calls

I have this code using (var stream = new StreamWriter(request.GetRequestStream(), Encoding)) stream.Write(body.ToString()); I need to make it asynchronous. As far as I understand it, this means I need to change the call to…
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304