26

My code in C# (asp.net MVC)

StreamWriter tw = new StreamWriter("C:\\mycode\\myapp\\logs\\log.txt");

// write a line of text to the file
tw.Write("test");

The file is created but is empty. No exception is thrown. I have never seen this before and I am stuck here; I just need to write some debugging output.

Please advise.

Noctis
  • 11,507
  • 3
  • 43
  • 82
sarsnake
  • 26,667
  • 58
  • 180
  • 286

8 Answers8

72

StreamWriter is buffered by default, meaning it won't output until it receives a Flush() or Close() call.

You can change that by setting the AutoFlush property, if you want to. Otherwise, just do:

StreamWriter tw = new StreamWriter("C:\\mycode\\myapp\\logs\\log.txt");

// write a line of text to the file
tw.Write("test");
tw.Close();  //or tw.Flush();
Benjamin Cox
  • 6,090
  • 21
  • 19
  • 2
    I had this same issue- couldn't figure out why it was not writing even though everything was correct. Did not know about the flush/close. I think this is the best answer overall. (I know this is old) – Nyra Dec 24 '14 at 15:56
8

You need to either close or flush the StreamWriter after finishing writing.

tw.Close();

or

tw.Flush();

But the best practice is to wrap the output code in a using statement, since StreamWriter implements IDisposable:

using (StreamWriter tw = new StreamWriter("C:\\mycode\\myapp\\logs\\log.txt")){
// write a line of text to the file
tw.Write("test");
}
pedram bashiri
  • 1,286
  • 15
  • 21
7

Neither flushed nor closed nor disposed. try this

using (StreamWriter tw = new StreamWriter(@"C:\mycode\myapp\logs\log.txt"))
{
    // write a line of text to the file
    tw.Write("test");
    tw.Flush();
}

or my preference

using (FileStream fs = new FileStream( @"C:\mycode\myapp\logs\log.txt"
                                     , FileMode.OpenOrCreate
                                     , FileAccess.ReadWrite)           )
{
    StreamWriter tw = new StreamWriter(fs);
    tw.Write("test");
    tw.Flush();
}
Noctis
  • 11,507
  • 3
  • 43
  • 82
Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • You don't need to run "tw.Flush()" in your first code piece. StreamWriter implements IDisposable with means when the using block ends, it is disposed of automatically. – pedram bashiri Aug 01 '19 at 18:46
  • Also in your second code piece, I would create tw in using statement too so I don't have to worry about flushing/closing it. – pedram bashiri Aug 01 '19 at 18:47
  • Flush will only happen if it's on automatic, and that wasn't there when I started using this pattern a lot. Just habit I developed back in olden times that stuck with me. – Tony Hopkinson Aug 02 '19 at 13:54
5

Use

System.IO.File.WriteAllText(@"path\te.txt", "text");
crassr3cords
  • 280
  • 1
  • 7
3
FileStream fs = new FileStream("d:\\demo.txt", FileMode.CreateNew,
                               FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.ASCII);
int data;

sw.Write("HelloWorld");

sw.Close();
fs.Close();
  • The problem is when StreamWriter Object is created with reference of FileStream Object , SW object will be always expecting some data till SW object is Closed.
  • So After using sw.Close(); Your Opened File will get closed and get ready for showing Output.
TioneB
  • 468
  • 3
  • 12
2

Ya in VB.net this was not needed but it seems with CSharp you need a Writer.Flush call to force the write. Of course Writer.Close() would force the flush as well.

We can also set the AutoFlush Property of the StreamWriter instance:

sw.AutoFlush = true;
// Gets or sets a value indicating whether the StreamWriter 
// will flush its buffer to the underlying stream after every  
// call to StreamWriter.Write.

From: http://msdn.microsoft.com/en-us/library/system.io.streamwriter.autoflush(v=vs.110).aspx

shivesh suman
  • 1,511
  • 18
  • 26
0

an alternative

FileStream mystream = new FileStream("C:\\mycode\\myapp\\logs\\log.txt",    
FileMode.OpenOrCreate, FileAccess.Write);           
StreamWriter tw = new StreamWriter(mystream); 
tw.WriteLine("test");
tw.close();
akjoshi
  • 15,374
  • 13
  • 103
  • 121
user1415567
  • 431
  • 5
  • 14
0

Try to close the file or add \n to the line such as

tw.WriteLine("test");
tw.Close();
Noctis
  • 11,507
  • 3
  • 43
  • 82
Hai Bi
  • 1,173
  • 1
  • 11
  • 21