FileWatcher - file in use by another process

R

Roger Twomey

I am working on a filewatcher application.

The premis is:

User uploads an xml file onto the web server
the filewatcher app sees the xml file
filewatcher app reads the file and inserts records into our sql server
filewatcher app deletes the file.

I am trying to figure out the best method of doing this, so I am adding code bit by bit. Currently I have this:
<code>
Private Sub fsWatcher_Created(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsWatcher.Changed
Dim strFileName As String

strFileName = e.FullPath.ToString

EventLog.WriteEntry(Me.ServiceName, Me.ServiceName & " File found = " & strFileName, EventLogEntryType.Information)


Try
File.Delete(strFileName)

Catch ex As Exception
EventLog.WriteEntry(Me.ServiceName, Me.ServiceName & " " & ex.ToString, EventLogEntryType.Error)
End Try

End Sub
</code>

You will notice that I write to the event log when I see the file, then I attempt to delete the file and if it fails I write to the eventlog.

Currently when I write to the event log I get a pattern like this:

File is found
Delete error (file in use by another process)

File is found
Delete error (file in use by another process)

File is found
Delete error (file in use by another process)

it should be noted that I specifically put the log entries 3 times because the do in fact show up 3 times. I don't know why, it certainly isn't logical to me. It should also be noted that SOMETIMES the file deletes, but USUALLY it does not.

Can anyone tell me:

1) Why is it being recorded 3 times.
2) How do I make the filewatcher 'let go' of the file so that I can delete it?

Unfortunately at this point it is rather urgent. I really appreciate any help you can give.

Thanks.
 
R

Roger Twomey

Thanks.

I will check that out. It's a start!

I would advice you to download the free FileMon from SysInternals (www.sysinternals.com), it will give you low level information about file access and might help you figure it out. I would anyhow guess that the underlying NTFS API attempt to access the file three times before giving up, thus giving you three log messages.
"Roger Twomey" <[email protected]> skrev i melding I am working on a filewatcher application.

The premis is:

User uploads an xml file onto the web server
the filewatcher app sees the xml file
filewatcher app reads the file and inserts records into our sql server
filewatcher app deletes the file.

I am trying to figure out the best method of doing this, so I am adding code bit by bit. Currently I have this:
<code>
Private Sub fsWatcher_Created(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsWatcher.Changed
Dim strFileName As String

strFileName = e.FullPath.ToString

EventLog.WriteEntry(Me.ServiceName, Me.ServiceName & " File found = " & strFileName, EventLogEntryType.Information)


Try
File.Delete(strFileName)

Catch ex As Exception
EventLog.WriteEntry(Me.ServiceName, Me.ServiceName & " " & ex.ToString, EventLogEntryType.Error)
End Try

End Sub
</code>

You will notice that I write to the event log when I see the file, then I attempt to delete the file and if it fails I write to the eventlog.

Currently when I write to the event log I get a pattern like this:

File is found
Delete error (file in use by another process)

File is found
Delete error (file in use by another process)

File is found
Delete error (file in use by another process)

it should be noted that I specifically put the log entries 3 times because the do in fact show up 3 times. I don't know why, it certainly isn't logical to me. It should also be noted that SOMETIMES the file deletes, but USUALLY it does not.

Can anyone tell me:

1) Why is it being recorded 3 times.
2) How do I make the filewatcher 'let go' of the file so that I can delete it?

Unfortunately at this point it is rather urgent. I really appreciate any help you can give.

Thanks.
 
I

Inge Henriksen

I would advice you to download the free FileMon from SysInternals (www.sysinternals.com), it will give you low level information about file access and might help you figure it out. I would anyhow guess that the underlying NTFS API attempt to access the file three times before giving up, thus giving you three log messages.
"Roger Twomey" <[email protected]> skrev i melding I am working on a filewatcher application.

The premis is:

User uploads an xml file onto the web server
the filewatcher app sees the xml file
filewatcher app reads the file and inserts records into our sql server
filewatcher app deletes the file.

I am trying to figure out the best method of doing this, so I am adding code bit by bit. Currently I have this:
<code>
Private Sub fsWatcher_Created(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsWatcher.Changed
Dim strFileName As String

strFileName = e.FullPath.ToString

EventLog.WriteEntry(Me.ServiceName, Me.ServiceName & " File found = " & strFileName, EventLogEntryType.Information)


Try
File.Delete(strFileName)

Catch ex As Exception
EventLog.WriteEntry(Me.ServiceName, Me.ServiceName & " " & ex.ToString, EventLogEntryType.Error)
End Try

End Sub
</code>

You will notice that I write to the event log when I see the file, then I attempt to delete the file and if it fails I write to the eventlog.

Currently when I write to the event log I get a pattern like this:

File is found
Delete error (file in use by another process)

File is found
Delete error (file in use by another process)

File is found
Delete error (file in use by another process)

it should be noted that I specifically put the log entries 3 times because the do in fact show up 3 times. I don't know why, it certainly isn't logical to me. It should also be noted that SOMETIMES the file deletes, but USUALLY it does not.

Can anyone tell me:

1) Why is it being recorded 3 times.
2) How do I make the filewatcher 'let go' of the file so that I can delete it?

Unfortunately at this point it is rather urgent. I really appreciate any help you can give.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top