473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1175
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>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.