Link to home
Start Free TrialLog in
Avatar of ilexas
ilexas

asked on

What process is holding a file?

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
Avatar of AlexFM
AlexFM

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.
Avatar of ilexas

ASKER

How can I "wait while file access is denied" ? how While loop would look lite? can you give me a code example?
Thanks
ASKER CERTIFIED SOLUTION
Avatar of _TAD_
_TAD_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
try
   {
       DoWork();
...
..
.


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