-2

^^^ That answer is in C#! I am using VB.NET! ^^^

I am using a FileSystemWatcher to monitor a folder for new files.

This sub gets triggered when it detects a change, which should then copy the file to the server.

   Private Sub OnCreated(source As Object, e As FileSystemEventArgs)
        Dim LocalFile As String = e.FullPath
        Dim ServerFile As String = LocalFile.Replace(localSyncPath, serverSyncPath)

        Try
            File.Copy(LocalFile, ServerFile)
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub

Problem is that this usually pops up an error to say the file is in use. Is there a way to run a loop to keep trying until it works? Or even copy dispite being in use?

MRC
  • 555
  • 2
  • 10
  • 30

1 Answers1

1

The link I gave for you to examine is easily translated to VB.NET:

Public Shared Sub listener_Created(sender As Object, e As FileSystemEventArgs)
    Console.WriteLine("File Created:" & vbLf + "ChangeType: " + e.ChangeType + vbLf & "Name: " + e.Name + vbLf & "FullPath: " + e.FullPath)
    Try
        File.Copy(e.FullPath, "D:\levani\FolderListenerTest\CopiedFilesFolder\" + e.Name)
    Catch
        _waitingForClose.Add(e.FullPath)
    End Try
    Console.Read()
End Sub

Public Shared Sub listener_Changed(sender As Object, e As FileSystemEventArgs)
    If _waitingForClose.Contains(e.FullPath) Then
        Try
            File.Copy(...)
            _waitingForClose.Remove(e.FullPath)
        Catch
        End Try
    End If
End Sub
Community
  • 1
  • 1
Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79
  • I seem I were looking at the top answer. I understand the idea now. the _waitingforclose will be a string array? – MRC Jan 13 '14 at 12:25
  • Yes. A string array or a list of strings will suffice. – Sani Huttunen Jan 13 '14 at 12:58
  • Is `listener_Changed` triggered when the file is closed? If it isn't, this code is just hiding the error. You shouldn't wrap stuff in `Try ... Catch` if you're not going to deal with the error. – Steven Liekens Jan 13 '14 at 13:01
  • @StevenLiekens It's triggered when the file has changed. When a file is created, it first triggers the oncreated event, then the data is written to it right after which triggers the onchanged event – MRC Jan 13 '14 at 13:03
  • What happens if the process that is creating/changing the file holds an exclusive lock on the file? Wouldn't that cause an exception? – Steven Liekens Jan 13 '14 at 13:06
  • @StevenLiekens I'm changing the way it works. I will monitor the events and put them into separate arrays. Every 30 seconds the script will loop though the arrays, removing the ones that have been completed. Anything with an extensive lock will remain in the array until it can be done. That sound good? – MRC Jan 13 '14 at 13:11
  • @StevenLiekens: There is no built in solution to the specific problem and this 'hack' is used to continuously poll the file to see if it can be accessed. When closing file (and releasing the lock) a `Last Write Time` or `Last Access Time` event should be triggered. – Sani Huttunen Jan 13 '14 at 13:11
  • @Sani so releasing the lock (if any) does actually trigger the event? Note that I genuinly don't know. I'm just curious. – Steven Liekens Jan 13 '14 at 13:15
  • @StevenLiekens: I genuinly don't know either. The above solution was just a translation from the link I gave to demonstrate that it's easy to translate C# to VB.NET. It would suprise me that no event is triggered when a file is closed, on the other hand there are several similar questions with the exact same problem and similar 'hacked' solutions. One thing for sure is that the framework has no built-in solution for this. – Sani Huttunen Jan 13 '14 at 13:18
  • 1
    In that case, I'd go with the other idea to retry every 30 seconds. That seems more reasonable to me than retrying only once whenever the file changes. – Steven Liekens Jan 13 '14 at 13:22
  • 1
    Done a quick test. Doesn't seem to fire the event when a file is unlocked. – MRC Jan 13 '14 at 13:22
  • @Joey: Also did a test and it does fire. Sometimes. You need to have `LastWrite`, `LastAccess` and `Security` `NotifyFilters`. – Sani Huttunen Jan 13 '14 at 13:39
  • Best option is still to poll at a certain time interval. – Sani Huttunen Jan 13 '14 at 13:47