472,358 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,358 software developers and data experts.

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 1136
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: murphy | last post by:
I'm programming a site that displays info from AWS Commerce Service 4.0. At each change of the asp.net application the first load of a page that uses the web service takes 30 seconds. Subsequent...
4
by: Claire | last post by:
Hi, I know this isn't strictly C# language related, but my service IS written in c# and I checked out the list of microsoft newsgroups for win2000 and couldn't really see one that applies. I'm...
3
by: JL | last post by:
I have a VB.NET desktop program that reads/writes data to a server using a Java-based Web Service. This web service, in identical formats, is located on several servers with each server being a...
3
by: Chris Dunaway | last post by:
I am writing a Windows Service that reads and processes files on a set schedule. I want to create a second app that can monitor the Windows service. The Windows service will write trace messages...
2
by: Trevor | last post by:
Argh! This problem is driving me nuts! Can you help? In November of 2003, I installed a web service on Windows Server 2003 built in VB.NET for v1.1.4322 of the framework. It contains a timer...
3
by: Terry Olsen | last post by:
I have a windows service that has been running fine for almost 6 months. Last week, it just stopped working. It is still in the task list and still shows "started" in the service manager. There...
0
by: mattmcgregor | last post by:
I'm using Visual Studio 2008 and have got a project with the following: Web Service (on a server): 1 web method that returns an xml list of files stored on the server. It references Component A...
5
by: Yash | last post by:
Hi, I am using SQL 2000 SP4. I have compared 2 scenarios: Scenario 1: insert into #bacs_report SELECT ..... WHERE <conditions>
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.