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.OnCreate d 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.EnableRaisingEvent s = True
Private Shared Sub OnCreated(ByVal source As Object, ByVal e As FileSystemEventArgs)
'do some parsing....
End Sub
My program has to parse *.txt file after file has been copied to "C:\temp\" folder
The problem is that FileSystemWatcher.OnCreate
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.EnableRaisingEvent
Private Shared Sub OnCreated(ByVal source As Object, ByVal e As FileSystemEventArgs)
'do some parsing....
End Sub
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.
ASKER
How can I "wait while file access is denied" ? how While loop would look lite? can you give me a code example?
Thanks
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try
{
DoWork();
...
..
.
the DoWork() function is where you handle your file... open, move, delete.. whatever.
{
DoWork();
...
..
.
the DoWork() function is where you handle your file... open, move, delete.. whatever.