Good day everyone,

I am trying to do a file lock (readonly) service to prevent two files from being modified at the same time. Files are sync between 2 Nas and can be opened at each end locally. Problem arise when they are saving the files as you might have guessed, last one to save get the final synch even if it is not the most up to date version. basically I want to use FileSystemWatcher to monitor the state of all files in a directory and subdirectories and when a file is opened it will change the read attribute of the remote file to ReadOnly. I am however running in a few bumps, FileSystemWatcher is great to monitor change, but can't read the file status (Opened, closed) and I need to know when the file is closed to remove the ReadOnly on the distant file and I am unsure if OnChange will trigger when closing the file.

Second I have never worked with services before and haven't code for a few years either, so please bare with me

here is the service part:

Code:
Imports System
Imports System.IO
Imports Microsoft.VisualBasic
Imports System.Security.Permissions



Public Class RemoteFileLocker
    Dim eventId As Long
    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.
        EventLog1.WriteEntry("In OnStart")
        Dim timer As System.Timers.Timer = New System.Timers.Timer()
        timer.Interval = 60000 ' 60 seconds
        AddHandler timer.Elapsed, AddressOf Me.OnTimer
        timer.Start()
    End Sub
    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
        EventLog1.WriteEntry("In OnStop.")
    End Sub

    Private Sub OnTimer(sender As Object, e As Timers.ElapsedEventArgs)
        'call the FileSystemWatcher
        Dim watchdog As Watcher
        watchdog = New Watcher
        EventLog1.WriteEntry("Monitoring the System", EventLogEntryType.Information, eventId)
        eventId = eventId + 1
    End Sub

End Class
and here is the FileSystemWatcherCode:

Code:
Public Class Watcher

    Public Shared Sub Main()

        Run()

    End Sub

    <PermissionSet(SecurityAction.Demand, Name:="FullTrust")>
    Private Shared Sub Run()

        Dim args() As String = System.Environment.GetCommandLineArgs()
        Dim path As String
        path = "G:\Work\Gates Innovation\Projet Prog\Test de sync fileLocker"
        ' If a directory is not specified, exit the program.
        'If args.Length <> 2 Then
        '    ' Display the proper way to call the program.
        '    Console.WriteLine("Usage: Watcher.exe (directory)")
        '    Return
        'End If

        ' Create a new FileSystemWatcher and set its properties.
        Dim watcher As New FileSystemWatcher()
        watcher.Path = path
        ' Watch for changes in LastAccess and LastWrite times, and
        ' the renaming of files or directories. 
        watcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)
        ' Include all subdirectories
        watcher.IncludeSubdirectories = True

        ' Add event handlers.

        AddHandler watcher.Changed, AddressOf OnChanged
        AddHandler watcher.Error, AddressOf OnError
        'AddHandler watcher.Created, AddressOf OnChanged
        'AddHandler watcher.Deleted, AddressOf OnChanged
        'AddHandler watcher.Renamed, AddressOf OnRenamed

        ' Begin watching.
        watcher.EnableRaisingEvents = True

        ' Wait for the user to quit the program.
        Console.WriteLine("Press 'q' to quit the sample.")
        While Chr(Console.Read()) <> "q"c
        End While
    End Sub

    ' Define the event handlers.
    Private Shared Sub OnChanged(source As Object, e As FileSystemEventArgs)
        ' Specify what is done when a file is changed, created, or deleted.
        'Console.WriteLine("File: " & e.FullPath & " " & e.ChangeType)
        Dim FileStat As String

        Select Case FileStat(e.FullPath)
            Case "vbUseDefault"
                'Write to log
            'if file is open
            Case "vbFalse"
                File.SetAttributes(e.FullPath, FileAttributes.ReadOnly)
            'if file is closed
            Case "vbTrue"
                File.SetAttributes(e.FullPath, File.GetAttributes(e.FullPath) & ~FileAttributes.ReadOnly)
        End Select

    End Sub
    ' Define error handler
    Private Shared Sub OnError(source As Object, e As IO.ErrorEventArgs)
        ' Get info about the error
        Console.WriteLine("An error occurred: " & e.GetException.Message)
    End Sub


End Class
Private Function FileStatus(ByVal FileName As String) As String
    Dim intFile As Integer

    On Error Resume Next
    GetAttr(FileName)
    If Err.Number Then
        FileStatus = vbUseDefault 'File doesn't exist or file server not available.
    Else
        Err.Clear()
        intFile = FreeFile(0) 'giving an error
        Open FileName For Binary Lock Read Write As #intFile 'giving an error
        If Err.Number Then
            FileStatus = vbFalse 'File already open.
        Else
            Close #intFile 'giving an error
            FileStatus = vbTrue 'File available and not open by anyone.
        End If
    End If
End Function