Service that Reads/Writes Files/SQL Server

D

DraguVaso

Hi,

I want to create a Service that Reads and Write Files, and Insert and Select
Data from a Sql Server Database.

I did a little test with a small Service that I wanted to write something to
a File when it starts and stops. I did it like this:
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
Dim swFile As StreamWriter =
File.AppendText("c:\ServicePieterP2.log")
swFile.WriteLine("blabla")
End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.
Dim swFile As StreamWriter =
File.AppendText("c:\ServicePieterP2.log")
swFile.WriteLine("blabla2")
End Sub


I'm able to create and install the service, but when I Start it, the File
"c:\ServicePieterP2.log" is created, but nothing is written in it!! And when
I try to stop the service I got an error (time-out).

Can anybody help me with this? Is it possible to write to a file with a
service? does anybody knows a good site about making Services in VB.NET that
reads/writes filesand Sql Server?

Thanks a lot in advance,

Pieter
 
H

Henrik_the_boss

Hello Pieter.

Should not be any problem.
As a service, you can enter credentials for the service, so that permissions
allow you to create the files, as has been done in your test.
If your service runs as an administrator, as you yourself probably are on
you own computer, there is no real difference in how you can access files
and object libraries between a service and a windows application.

From the code you submitted though, it seems that the problem lies in the
StreamWriter code.
A stream is not persisted to disk until you flush it.
Also it is good practice to close the stream before exiting the function.

The code should look like this:.

Dim swFile As StreamWriter = File.AppendText("c:\ServicePieterP2.log")

swFile.WriteLine("blabla")
swFile.Flush()
swFile.Close()
swFile = Nothing

From MSDN:
You must call Close to ensure that all data is correctly written out to the
underlying stream. Following a call to Close, any operations on the
StreamWriter might raise exceptions.

Flushing the stream will not flush its underlying encoder unless you
explicitly call Flush or Close. Setting AutoFlush to true means that data
will be flushed from the buffer to the stream, but the encoder state will
not be flushed. This allows the encoder to keep its state (partial
characters) so that it can encode the next block of characters correctly.
This scenario affects UTF8 and UTF7 where certain characters can only be
encoded after the encoder receives the adjacent character or characters.


// Henrik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top