Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

monitoring a file

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
  • 2
1 Solution
Hey there
Did a quick search and this might be of help. You could add additional logic in the catch section depending on your requirements.

Hope this helps
If it does, please mark as answer

Public Function IsFileLocked(file As FileInfo) As Boolean
    Dim stream As FileStream = Nothing
	stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None)
    Catch ex As IOException
	'the file is unavailable because it is:
	'  still being written to
	'  or being processed by another thread
	'  or does not exist (has already been processed)
	Return True
	If stream IsNot Nothing Then stream.Close()
    End Try

    'file is not locked
    Return False
End Function

Open in new window

cano63Author Commented:
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
Dim FS As IO.FileStream = IO.File.Open(FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
FS = Nothing
Timer1.Enabled = false
Call My Api process
Catch ex As IO.IOException
' FIle is Lock
Catch ex As Exception
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.


Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now