Solved

What process is holding a file?

Posted on 2004-04-07
4
1,054 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
Comment Utility
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
Comment Utility
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
Comment Utility

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_
Comment Utility
try
   {
       DoWork();
...
..
.


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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now