469,578 Members | 1,257 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

Service that Reads/Writes Files/SQL Server

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
Nov 20 '05 #1
1 1069
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


"DraguVaso" <pi**********@hotmail.com> wrote in message
news:eg**************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by murphy | last post: by
4 posts views Thread by Claire | last post: by
3 posts views Thread by Chris Dunaway | last post: by
2 posts views Thread by Trevor | last post: by
3 posts views Thread by Terry Olsen | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.