monitoring a file

Posted on 2011-10-07
Last Modified: 2012-05-12
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
Question by:cano63
    LVL 2

    Accepted 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

    LVL 1

    Author Comment

    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

    LVL 2

    Expert Comment

    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

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now