Questions tagged [textwriter]

TextWriter is an abstract base within the .NET Framework, and represents a writer that can write a sequential series of characters.

73 questions
24
votes
2 answers

Implementing a Derived Class of TextWriter

I have two classes, none of which I can change in any way: Class 1: Takes a TextWriter as constructor parameter and uses it as an output stream. Class 2: Provides a method WriteLine(string). I need an adapter, such that all the output of Class1 is…
D.R.
  • 20,268
  • 21
  • 102
  • 205
23
votes
5 answers

Sharing violation IOException while reading and writing to file C#

Here is my code: public static TextWriter twLog = null; private int fileNo = 1; private string line = null; TextReader tr = new StreamReader("file_no.txt"); TextWriter tw = new StreamWriter("file_no.txt"); line = tr.ReadLine(); if(line != null){ …
Ozg
  • 391
  • 2
  • 7
  • 20
13
votes
5 answers

Writing a Text File in memory and saving it with savefiledialog

I am trying to make a text file in memory, add some lines to it and at the end save the file in a text file. I can handle the savedialog part but I dont know how to get the text file from memory. Any help and tips will be appriciated. What I am…
Dumbo
  • 13,555
  • 54
  • 184
  • 288
10
votes
2 answers

Writing Unix style text file in C#

I'm trying to write a text file with Unix-style newlines with my C# program. For some reason the following code doesn't work: TextWriter fileTW = ... fileTW.NewLine = "\n"; fileTW.WriteLine("Hello World!"); Neither does this: TextWriter fileTW =…
dan6470
  • 309
  • 2
  • 4
  • 9
10
votes
3 answers

Efficient way to write a lot of lines to a text file

I started off doing something as follows: using (TextWriter textWriter = new StreamWriter(filePath, append)) { foreach (MyClassA myClassA in myClassAs) { textWriter.WriteLine(myIO.GetCharArray(myClassA)); if…
lintmouse
  • 5,079
  • 8
  • 38
  • 54
10
votes
2 answers

Setting the color for Console.Error writes

I’m writing a console application that has a custom logger in it. The logger needs to color anything sent to Console.Error in red. I now have third party references that also write to Console.Out and Console.Error so I have to do this in a way that…
TugboatCaptain
  • 4,150
  • 3
  • 47
  • 79
8
votes
2 answers

How to output Byte Order Mark when writing to TextWriter?

i am writing text to a TextWriter. i want the UTF-16 Byte Order Mark (BOM) to appear in the output: public void ProcessRequest(HttpContext context) { context.Response.ContentEncoding = new UnicodeEncoding(true, true); …
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
8
votes
1 answer

C# TextWriter, allow reading of files:

using (TextWriter writer = File.CreateText(path2)) { writer.Write(SomeText); } This is problematic piece of code. When I write to file, it's ok, until other app open the…
el ninho
  • 4,183
  • 15
  • 56
  • 77
5
votes
2 answers

Is there a TextWriter child class that fires event if text is written?

I have written a method that accepts a TextWriter as an argument (Usually Console.Out, but not necessarily). When I call this method, some progress info is written to the TextWriter. However, since this method can run a long time, I want to update…
Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
5
votes
1 answer

Force no BOM when saving XML

I'm saving a XML file that is used by an external tool. The tool sadly doesn't understand the encoding BOM (Byte Order Mark: EF BB BF) at the beginning of the file:  ... 00000000h: EF BB BF 3C 3F 78 6D 6C 20…
joe
  • 8,344
  • 9
  • 54
  • 80
4
votes
5 answers

Cannot write to a closed TextWriter

I am trying to write text to my txt file. After the first write the application crash with error Cannot write to a closed TextWriter My list contains links that the browser opens and I want to save all of them in txt file (like a log). My…
user1269592
  • 691
  • 3
  • 12
  • 24
4
votes
3 answers

How do you use a .Net TextWriter with Azure WebJobs

I want to do some simple debugging, and Console.Writeline no longer seems to be supported by Azure WebJobs. I know using a TextWriter class is the answer, and I just inject it into my method. What I don't understand is how I call that method. I…
Russ Taylor
  • 360
  • 3
  • 10
3
votes
1 answer

Is there a way to check if a TextWriter is closed?

I'm writing a class that exports data to a CSV file, and its constructor takes in a TextWriter. The reason I'm using a TextWriter rather than a StreamWriter is so that it would make testing easier: I can use the same constructor for writing to a…
9a3eedi
  • 696
  • 2
  • 7
  • 18
3
votes
1 answer

Using TextWriter.Synchronized or having lock in multithreaded file IO - C# WPF .net 4.5

Ok there are 2 ways of writing text to a file in multi threaded system Which one is better than other First case : having a static object to lock streamwriter and do operations Second case having : TextWriter twWaitingToBeFetched; …
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
3
votes
2 answers

Redirecting a Stream to a Text Box in real-time

I have got an interesting dilemma where my application can run as a Console App or a Windows Forms App. Since I do not want to write a shed load of code like this all over my application: If ( IsConsoleApp() ) { // process Console input and…
Intrepid
  • 2,781
  • 2
  • 29
  • 54
1
2 3 4 5