473,396 Members | 1,775 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,396 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 3197

"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.