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
5
votes
2 answers

C# Application Relative Paths

I just started learning C# and it looks like when you are writing an output file or reading an input file, you need to provide the absolute path such as follows: string[] words = { "Hello", "World", "to", "a", "file", "test" }; using…
Jack Frye
  • 583
  • 1
  • 7
  • 28
5
votes
5 answers

Thread safe StreamWriter C# how to do it? 1

What is the best way to build a program that is thread safe in terms that it needs to write double values to a file. If the function that saves the values via streamwriter is being called by multiple threads? Whats the best way of doing it? Code…
user349026
5
votes
1 answer

C# - StreamWriter Is Creating My File But No Content

I'm trying to use StreamWriter to log a search every time someone searches on my program. I can get StreamWriter to write a new file but it does not create any content. I've tried searching google for the proper use and to me it looks like I've done…
brutalitea
  • 55
  • 1
  • 5
5
votes
1 answer

Difference between using StreamWriter constructor and File.CreateText

What's the difference (CPU usage, MSIL, etc) between: StreamWriter sw = new StreamWriter("C:\test.txt"); and: StreamWriter sw = File.CreateText("C:\test.txt"); ?
Keyo
  • 119
  • 2
  • 8
5
votes
4 answers

C# StreamWriter and StreamReader memory managment problem, why won't memory used deallocate?

So I'm using a StreamReader that uses a MemoryStream to write to a StreamWriter and inside of this application but the memory usage increases by 300mb (From one of the larger inputs) and does not deallocate after Im done using it: StreamWriter log =…
Sam F
  • 621
  • 1
  • 8
  • 16
5
votes
2 answers

What is the right CSV encoding for c# to open with Excel?

I write a CSV with my C# CSVWriter based on a StreamWriter. In my data I have a lot of special characters like "Bávaro". So when I use UTF-8 or ASCII to encode my CSV I can't get the "á" but I can open it in Excel afterwards perfectly. When I use…
PassionateDeveloper
  • 14,558
  • 34
  • 107
  • 176
5
votes
4 answers

Creating text file in C#

I'm learning how to create text file in C# but I have a problem. I used this code: private void btnCreate_Click(object sender, EventArgs e) { string path = @"C:\CSharpTestFolder\Test.txt"; if (!File.Exists(path)) { …
TheZerda
  • 119
  • 1
  • 2
  • 8
5
votes
2 answers

Alternative/faster method to slow StreamWriter to print strings to a file in C#

Some help from experts. I'm trying to use the function below to print strings to a file. When I use Console.Write() or Console.WriteLine() the output file grows up 3MB or 4MB per seconds, but when I try to use StreamWriter or File.AppendAllText the…
Sarmeu
  • 95
  • 1
  • 3
  • 9
5
votes
2 answers

Inheriting from StreamWriter with smallest possible effort

I am using an external library which allows logging using StreamWriter - now I want to add some handling based on the content of the logging. As I want to avoid to loop through the log file, I would like to write a class which inherits from…
weismat
  • 7,195
  • 3
  • 43
  • 58
5
votes
7 answers

how to clear text file content c#

i want clear text file contet with this method private void writeTextFile(string filePath, string text) { if (!File.Exists(filePath)) { File.Create(filePath).Close(); } using (StreamWriter tw = new StreamWriter(filePath)) …
user3281649
  • 61
  • 1
  • 1
  • 3
5
votes
2 answers

"The given path's format is not supported" when trying to create file based on current time

Is there any way to save a file with a variable name? For example, I have this code but I got error in the StreamWriter: string hour = DateTime.Now.ToString("HH:mm:ss"); string date = DateTime.Now.ToShortDateString(); string filename = "Numbers " +…
João Silva
  • 531
  • 4
  • 21
  • 40
5
votes
3 answers

File being used by another process using StreamWriter

My program was practice for me, however, when I try to write all the directories it found, it crashes. I tried the following: Having it write to a file stream instead of the file itself using File.Writealllines using a list<> (this worked, only it…
Chris Altig
  • 680
  • 3
  • 8
  • 22
5
votes
1 answer

Specify position to write to a file using Writable Stream in nodejs

I want to write to a file multiple times using the Writable Stream in nodejs. The problem is that I also want to specify the postion also to this writer. Here is some background - I want to create a multi threaded downloader. The data which comes as…
tusharmath
  • 10,622
  • 12
  • 56
  • 83
5
votes
1 answer

How to write Arabic, Hebrew Into CSV file?

I can't write to Results.csv any of the languages Arabic or Hebrew, except English. Each time I'm trying to write any one of them I'm getting gibberish marks, in the CSV file where it should have proper Arabic or Hebrew words instead. I've been…
Roy Doron
  • 585
  • 9
  • 23
5
votes
1 answer

Writing to a file on Amazon s3 bucket using c#

I have a text file on my amazon s3 bucket and I want to write some text at the end of that file. Something like following : xxxxxx|xxxxxx|xxxxx|xxxx. I want to do this with c#. I did tried using streamwriter but it did not worked. using(StreamWriter…
slonkar
  • 4,055
  • 8
  • 39
  • 63