Solved

What process is holding a file?

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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