473,320 Members | 1,953 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,320 software developers and data experts.

FileSystemWatcher in Windows Service

I created a Windows Service (written in VB.NET) that uses a FileSystemWatcher
to monitor a directory for file creation. When files with a certain
extension are created in the directory, the file is opened and read
(something is done with the contents). Then the file is moved to a backup
directory. Everything works fine when the coe runs as a standard Windows app,
but when I run the code as a Windows Service, I get the following error when
I try to open the file:

The process cannot access the file "C:\Data\tests.txt" because it is being
used by another process.

Here is the VB code (FileSystemWatcher Created Event happens as it should):

....
Dim objFile As New FileInfo(fullPath)
Dim strContents As String
Dim objStream As StreamReader

' *** Error Happens on the next line ***
objStream = objFile.OpenText()

strContents = objStream.ReadToEnd()

' Close the stream
objStream.Close()
Thanks for any suggections.

Jul 21 '05 #1
2 3191

"Bill" <Bi**@discussions.microsoft.com> wrote in message
news:BD**********************************@microsof t.com...
I created a Windows Service (written in VB.NET) that uses a
FileSystemWatcher
to monitor a directory for file creation. When files with a
certain
extension are created in the directory, the file is opened and
read
(something is done with the contents). Then the file is moved
to a backup
directory. Everything works fine when the coe runs as a
standard Windows app,
but when I run the code as a Windows Service, I get the
following error when
I try to open the file:

The process cannot access the file "C:\Data\tests.txt" because
it is being
used by another process.


My first guess would be that your service is launching and trying
to work with the file before the process that put it there is
finished with it. When the file is put in the directory in the
first place, the process placing it there needs to be finished
and close the file, before your service will be allowed to
manipulate it.

Andrew Faust
Jul 21 '05 #2
If you want to detect whether the process that created the file has finished
writing to it (and closed it), you could periodically (in a loop, perhaps)
attempt to open it using something like:

Stream stream = null;
while (stream == null)
{
try
{
if (!File.Exists(path))
break;

stream = File.Open(path, FileMode.Open, FileAccess.Read,
FileShare.None);
}
catch (IOException)
{
Thread.Sleep(100);
}
}

That will cause your thread to pause until your application can gain
exclusive access to the file. You may want to add a time out etc.

Hope that helps.

"Andrew Faust" wrote:

"Bill" <Bi**@discussions.microsoft.com> wrote in message
news:BD**********************************@microsof t.com...
I created a Windows Service (written in VB.NET) that uses a
FileSystemWatcher
to monitor a directory for file creation. When files with a
certain
extension are created in the directory, the file is opened and
read
(something is done with the contents). Then the file is moved
to a backup
directory. Everything works fine when the coe runs as a
standard Windows app,
but when I run the code as a Windows Service, I get the
following error when
I try to open the file:

The process cannot access the file "C:\Data\tests.txt" because
it is being
used by another process.


My first guess would be that your service is launching and trying
to work with the file before the process that put it there is
finished with it. When the file is put in the directory in the
first place, the process placing it there needs to be finished
and close the file, before your service will be allowed to
manipulate it.

Andrew Faust

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lloyd Sheen | last post by:
I have created a FileSystemWatcher application that monitors a set of folder for changes. This works well as a windows app. I have copied the code from the windows app to a windows service and...
1
by: Rich Miller | last post by:
Previously posted on the VB newsgroup with no replies, so any ideas appreciated. I have written a service application in VB (VS.Net 2003). On start, the service creates an object that...
3
by: Craig Thompson | last post by:
I've attempted to write a windows service that creates one FileSystemWatcher for each entry in a XML config file. Everything start perfrectly and runs as I expect for about 5 minutes and then...
2
by: Jet Leung | last post by:
Hi all, I had made a program to watching files in my directory. I had used a instance of FileSystemWatcher to do my work.And I had add some events of the FileSystemWatcher , for example onChange,...
2
by: Bonnett | last post by:
I have made a simple console application that creates a webpage from my playlist, then uploads it to my webhost. I decided to convert it to a windows service and use the FileSystemWatcher to...
1
by: Phil396 | last post by:
I have a windows service that uses a filesystemwatcher to wait for files and process them to a database. Sometimes a large group of files will be cut and paste for the filesystemwatcher to...
2
by: Sacha Korell | last post by:
I need to set up a FileSystemWatcher in my web application (to automatically process uploaded files) and I'm trying to decide where to set it up. I would like to keep it within the web app, but it...
20
by: J-T | last post by:
We are working on an asp.net application which is a 3-tier application.I was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business...
0
by: Rich Miller | last post by:
Any ideas appreciated. I have written a service application in VB (VS.Net 2003). On start, the service creates an object that instantiates a System.IO.FileSystemWatcher like so (the _watcher is...
1
by: D2 | last post by:
Hi, We are using FileSystemWatcher class in a windows service to monitor a directory "d:\abc". This path is configured in a config file. When the service is running and FSW is watching this...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.