Link to home
Start Free TrialLog in
Avatar of cano63
cano63

asked on

monitoring a file

Hello,
I have create a windows service that monitor all the files that are created in a specific directory when a new file is creted I call an application api that will load the new file, the problem that ,I,m having is that is the file.still open by other application i will not  be able to upload the file to my api, Im looking a way to get to know when a file ia not more used by any other app so i can call my api
ASKER CERTIFIED SOLUTION
Avatar of the_o
the_o

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

ASKER

Thanks,
THe code work greate,

But i continue havien a problem

I,m using  the System.IO.FileSystemWatcher() to monituring my folder, when a folder is created and it detected  then I try to turn on my timer but it keep looping inside my logchange sub without entering in my timer.

I atach the code
Private Sub btn_startwatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_startwatch.Click
watchfolder = New System.IO.FileSystemWatcher()
watchfolder.Path = "C:\"
AddHandler watchfolder.Created, AddressOf logchange
watchfolder.EnableRaisingEvents = True
End Sub

Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Created Then
 File = e.FullPath
 Timer1.Enabled = True
End If
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim FileName As String = File
Try
Dim FS As IO.FileStream = IO.File.Open(FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
FS.Close()
FS.Dispose()
FS = Nothing
Timer1.Enabled = false
Call My Api process
Catch ex As IO.IOException
' FIle is Lock
Catch ex As Exception
'Error
End Try
End Sub

Open in new window

Hi there

A couple of notes.
Seeing that you are attaching the logchange sub to the Created event of watchfolder, there is no need to test again within the sub, the change type is .created as that is the event that fired and being handled

Also look at setting watcher.notifyfilter to relevant events to track
You can also try and invoke the timer.start method to ensure that the timer has started

Hope this helps as I havn't worked with the FSW in a couple of years.

Cheers
Theo