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
-2
votes
1 answer

Cant figure out how to seach items in a ListBox and return a result

I'm trying to create an application that searches for names in a listbox. Basically, I have 2 .txt files (BoyNames, GirlNames) one file contains a set of boy names and the other girl names. Ive managed to display the boy names to boyNameListBox and…
Relaxsingh
  • 11
  • 5
-2
votes
1 answer

C# Streamwriter after Flus or Dispose or Close, use again?

I have programmed a C# console application with a StreamWriter: Now I want to close the StreamWriter to write all elements in filename.txt but I want to use the same StreamWriter again, how can I do it? Open after Close? Example: StreamWriter Stream…
Hesoyam
  • 49
  • 7
-2
votes
4 answers

c# - How to create a file if it doesn't exist yet and add new text?

My code works fine for creating a file if it doesn't exist and inserting new text, or if the file already exists, it rewrites the current contents. path = @"C:\MY FOLDER\data.txt"; FileStream fileS = null; bool done = false; while (!done) { …
Totallama
  • 418
  • 3
  • 10
  • 26
-2
votes
1 answer

StreamWriter not writing all characters

I have this bit of code run when people close the game to save all their currently selected pets (this is for school don't worry about how I named it "Squirtle", no copyright problems here). The code in question is: using (StreamWriter sw =…
Matthew Young
  • 151
  • 1
  • 1
  • 12
-2
votes
2 answers

How do I write to txt file after code executed

I have added a Groceries.txt file to my project which contains a list of items (eg. regular,bread,2.00,2) which translates to type, item, cost, quantity. My project contains the code of what needs to be actioned per each class. I have created an…
McKimmie79
  • 1
  • 1
  • 1
-2
votes
1 answer

Program does not contain a static 'Main' method test_test suitable for an entry point, C#

CS 5001: Program does not contain a static 'Main' method test_test suitable for an entry point. The above error is given then I try to run the program along with an error attracted to the txt file call in the main stating: there is no argument…
-2
votes
2 answers

Appending a line to a hosts file ONLY if it doesnt already exist

My code looks like this; using (StreamWriter w = File.AppendText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers/etc/hosts"))) { w.WriteLine("127.0.0.1 www.google.com"); } I want get rid of duplicates in hosts…
Wiii
  • 3
  • 2
-2
votes
2 answers

Why I can't write on my Excel file with savefiledialog?

I need you to clear my point. I can use the application in SaveFileDialog stupid here but I'm a bit stuck. Basically, I have a OpenFileDialog to search .xml file and displays it in a datagridview. Then I will wish to export the contents of the…
-2
votes
1 answer

stream writer not logging append data

I needed to log the value of a int (camera tracking application). And so i wrote the following function, tried some variation but so far it only logs 1 data sample, while another command in my main app that refreshes a label does frequently update,…
Peter
  • 2,043
  • 1
  • 21
  • 45
-2
votes
1 answer

How do I write to a .cs file in c# and/or how to change the .txt to .cs

I want to write to a .cs file in C#. This is the (very basic) code I'm using: StreamWriter file = new StreamWriter ("test.cs"); file.WriteLine ("Console.WriteLine(\"It worked\")"); It successfully creates the file, but doesn't write anything to…
Mindstormer
  • 299
  • 3
  • 16
-2
votes
1 answer

Add column to Excel

using (System.IO.StreamWriter writer = new System.IO.StreamWriter(@"" + textBox2.Text + @"\" + filename.TrimStart() + ".csv", true)) { if (!exists) { …
dilhan
  • 19
  • 1
  • 7
-2
votes
1 answer

Dynamicly showing file contents in textbox [WPF]

im building an WPF application in which i have a textbox, but the problem is that this textbox needs to get the contents of a file as text, only the txt file keeps getting written too. i have made a class to handle this: public class ChatHandler { …
-2
votes
1 answer

write file out of memory c#

I get some problems with c# windows form. My goal is to slice a big file(maybe>5GB) into files,and each file contains a million lines. According to the code below,I have no idea why it will be out of memory. Thanks. StreamReader readfile = new…
yin
  • 13
  • 3
-2
votes
1 answer

How can I use Streamwriter to write in one line and whenever called again, to essentially delete that line and write on it again

Another way to say it is this: I want a program that logs the amount of times you've pressed a button. To do this I'd need a StreamWriter to write to a text document the number of times and a StreamReader to read the number of times as to display it…
Nether
  • 1,081
  • 10
  • 14
-2
votes
2 answers

Rename existing method returning void with input arguments

Maybe this is silly, but I'm trying to shorten the calling of the method StreamWriter.WriteLine becasue I have to call it many times throughout my code. So, instead of calling myFile.WriteLine() I would like to write just myFile.WL(). Is this…
Leonardo Trivino
  • 295
  • 1
  • 4
  • 11
1 2 3
99
100