Re: Windows Service and StreamWriter



Hello MATT,

I am trying to create a windows service. The part I am having trouble
with is writing text to a log file. I am using a very basic
StreamWriter function to try to test this.

I have created a very basic service app to try to test this:

OnStart
timer1.enabled = True
OnStop
Timer1.enabled = Fales
Private Sub Timer1.elapsed (ByVal...blah blah blah) Handles blah

Dim fw as new StreamWriter("C:\LogFile", True)
fw.WriteLine("This is a test from the service")
End sub

but, of course, the text file is blank. Please help.

The problem is that StreamWriter buffers it's data in memory. You have to force it to push the data onto disk by calling the Close method. A better way is to use the "Using" statement which will force a call to Close (via IDisposable) even in the presence of an exception

Using ( fw As New StreamWriter("C:\LogFile", True))
....
End Using

--
Jared Parsons [MSFT]
jaredpar@xxxxxxxxxxxxxxxxxxxx
All opinions are my own. All content is provided "AS IS" with no warranties, and confers no rights.



.