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
Visual Basic.NET

Avatar of undefined
Last Comment
the_o

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
the_o

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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

the_o

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
Your help has saved me hundreds of hours of internet surfing.
fblack61