Solved

What process is holding a file?

Posted on 2004-04-07
4
1,062 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

718 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