Author Topic: FileSystemWatcher & checking for FileUploadComplete  (Read 1212 times)

bachphi

  • Hero Member
  • *****
  • Posts: 642
    • View Profile
FileSystemWatcher & checking for FileUploadComplete
« on: June 09, 2017, 12:07:26 PM »
Using FileSystemWatcher, one can monitor a folder for file creation, next making sure file upload complete before processing the file.
Code: [Select]
Public Function FileUploadCompleted(filename As String) As Boolean
        ' If the file can be opened for exclusive access it means that the file
        ' is no longer locked by another process.
        Try
            Using inputStream As FileStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.None)
                Return True
            End Using

        Catch generatedExceptionName As IOException
            Return False
        End Try
End Function

using a While loop, one can check for File Upload complete, but sleep time is not an efficient way, would you have a more efficient way to do this:
Code: [Select]
While Not FileUploadCompleted(e.FullPath)
            System.Threading.Thread.Sleep(500)
End While
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: FileSystemWatcher & checking for FileUploadComplete
« Reply #1 on: June 09, 2017, 12:58:51 PM »
Does the FileSystemWatcher fire multiple events such as when the file is first created, then subsequent times when it is changed as data is added to it? I am wondering if trying to open the file in the file changed event will fail multiple times until the file lock is released which would mean the upload is complete.

bachphi

  • Hero Member
  • *****
  • Posts: 642
    • View Profile
Re: FileSystemWatcher & checking for FileUploadComplete
« Reply #2 on: June 09, 2017, 02:41:32 PM »
The watcher only watch for created event and not changed, deleted, renamed.  Currently, it does not use any NotifyFilter, I think it could have used NotifyFilter.LastWrite
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: FileSystemWatcher & checking for FileUploadComplete
« Reply #3 on: June 09, 2017, 03:03:19 PM »
Here is something along the lines that I was thinking, but not sure if it will actually work:
Code: [Select]
   Private FirstCreatedFile As String
    Private Sub FileSystemWatcher1_Created(sender As Object, e As IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
        Console.WriteLine(Now & " File created " & e.FullPath)
        If String.IsNullOrEmpty(FirstCreatedFile) Then FirstCreatedFile = e.FullPath
    End Sub

    Private Sub FileSystemWatcher1_Changed(sender As Object, e As IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
        Console.WriteLine(e.FullPath & " has changed")
        If e.FullPath = FirstCreatedFile Then
            Console.WriteLine(Now & " Changed")
            Try
                Using sr As New System.IO.StreamReader(e.FullPath)
                    Console.WriteLine(Now & " Ready")
                End Using
            Catch ex As Exception
                Console.WriteLine(Now & " Not Ready. " & ex.Message)
            End Try
        End If
    End Sub

bachphi

  • Hero Member
  • *****
  • Posts: 642
    • View Profile
Re: FileSystemWatcher & checking for FileUploadComplete
« Reply #4 on: June 09, 2017, 04:31:03 PM »
That might works! more logical than using sleep time for sure. Thanks Archie.
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

bachphi

  • Hero Member
  • *****
  • Posts: 642
    • View Profile
Re: FileSystemWatcher & checking for FileUploadComplete
« Reply #5 on: June 13, 2017, 07:51:51 PM »
This is what I ended up with:
Code: [Select]
Imports System.IO

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim fsw As New System.IO.FileSystemWatcher
        fsw.Path = Application.StartupPath
        fsw.Path = fsw.Path.Substring(0, fsw.Path.LastIndexOf("\")) + "\XferedData"
        fsw.NotifyFilter = (NotifyFilters.LastWrite Or NotifyFilters.FileName)
        fsw.Filter = "*.txt"

        AddHandler fsw.Changed, New FileSystemEventHandler(AddressOf OnChanged)
        AddHandler fsw.Created, New FileSystemEventHandler(AddressOf OnChanged)

        fsw.EnableRaisingEvents = True

    End Sub

 

    Private Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)

        Dim wct As WatcherChangeTypes = e.ChangeType
        Debug.WriteLine("File :  {0} {1}", e.FullPath, wct.ToString())
        Try
            Using inputStream As FileStream = File.Open(e.FullPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
                Debug.WriteLine(Now & " READY ")
            End Using

        Catch ex As IOException
            Debug.WriteLine(Now & " NOT READY YETT " & ex.Message)
        End Try

    End Sub


End Class
« Last Edit: July 25, 2017, 09:29:56 PM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================