Hi Kevien,
When you write using a StreamWriter, you are just writing to the buffer,
not to the underlying stream. This buffer is flushed to the underlying
stream using Flush or Close, or by setting AutoFlush = true.
When you call debugWriter.Close the buffer is flushed to the file.
A good method to ensure you flush the buffer is the 'using' statement
which will automatically call close/dispose on the object when it goes out
of scope.
static void Main(string[] args)
{
FileInfo file = new FileInfo(@"E:\Demo\Log\20061214.log");
using(StreamWriter debugWriter = new
StreamWriter(file.Open(FileMode.Append, FileAccess.Write,
FileShare.ReadWrite)))
{
debugWriter.Write("dasdasdasdsadsadsadasdasd123354 856");
}
}
On Thu, 14 Dec 2006 09:46:03 +0100, Kevien Lee <ir************@gmail.com
wrote:
Hi ,
I had a strang problam ,when i use StreamWriter to append to a file,i
found that if i don't close the
StreamReader it couldn't write the data into file,the code as folllow
that:
class Program
{
static void Main(string[] args)
{
FileInfo file = new FileInfo(@"E:\Demo\Log\20061214.log");
StreamWriter debugWriter = new
StreamWriter(file.Open(FileMode.Append, FileAccess.Write,
FileShare.ReadWrite));
debugWriter.Write("dasdasdasdsadsadsadasdasd123354 856");
debugWriter.Close();
}
}
however,if remove the debugWriter.Close(),the problam show,why ?can
anyone help me?
Thanks
--
Happy Coding!
Morten Wennevik [C# MVP]