سؤال

^^^ 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?

هل كانت مفيدة؟

المحلول

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top