Solved

What process is holding a file?

Posted on 2004-04-07
4
1,057 Views
Last Modified: 2010-08-05
Does anyone know how to find out what process is holding a file?
 
My program has to parse *.txt file after file has been copied to "C:\temp\" folder
The problem is that FileSystemWatcher.OnCreated evet fires the moment file is created (while still it is being copied)

I get an error "Additional information: The process cannot access the file because it is being used by another process."

Dim watcher As New FileSystemWatcher

watcher.Filter = "*.txt"
watcher.Path = "C:\temp\"
AddHandler watcher.Created, AddressOf OnCreated
watcher.EnableRaisingEvents = True

Private Shared Sub OnCreated(ByVal source As Object, ByVal e As FileSystemEventArgs)
   'do some parsing....
End Sub
0
Comment
Question by:ilexas
  • 2
4 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10775233
I don't think you need to find the process which holds the file. All you need is to wait while file access is denied. You can create worker thread for each file and try to open file in this thread. If you get "Access denied", sleep some time and try again. Define some maximal time for this loop. By doing this you can exclude the case when file is created and kept by some program for a long time.
0
 
LVL 1

Author Comment

by:ilexas
ID: 10775411
How can I "wait while file access is denied" ? how While loop would look lite? can you give me a code example?
Thanks
0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 500 total points
ID: 10775564

While I agree with Alex, I would add an extra step.

Catching errors is a rather expensive feature, and using the "Catch" feature to handle known/expected errors is not a good habit to get in to.

I would do exactly what Alex suggests, but I would check the creation date and time first, and not access it if the time is within say... 15 seconds of current time.



something like this:

using System;
using System.Threading;


{psedo code}

<FileSystemWatcher Event -- Found a file!>

Get the file's creation time
if fileDateTime + 15 seconds > GetDateTime.Now Then
     Thread.Sleep((GetDateTime.Now - fileDateTime + 15) * 1000) // so it never waits more than 15 seconds

while(true)
{
   int cnt = 0;
   try
   {
       DoWork();
       break;  // jumps out of while loop
   }
   catch
   {
       if (++cnt < 5)
           Thread.Sleep(5000); // sleep for 5 seconds    
       else
             Throw New Error --- file too busy
   }
}

0
 
LVL 22

Expert Comment

by:_TAD_
ID: 10775579
try
   {
       DoWork();
...
..
.


the DoWork() function is where you handle your file... open, move, delete.. whatever.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question