VB.NET – Créer un service permettant de savoir si un fichier à été modifié

Ajouter un try / catch ou un booléen dans les événements myFsWatcher_Created / myFsWatcher_Changed car pendant l’envoi le processus utilise le fichier et donc le service se met en erreur et s’arrête.

Imports System.IO

Imports Microsoft.VisualBasic

Imports System.Security.Permissions

Public Class Service1

Protected Overrides Sub OnStart(ByVal args() As String)

‘ Ajoutez ici le code pour démarrer votre service. Cette méthode doit

‘ démarrer votre service.

initFsWatcher()

End Sub

Protected Overrides Sub OnStop()

End Sub

Private WithEvents myFsWatcher As New FileSystemWatcher

Private folderToWatch As String = “c:\test\”

Private Sub initFsWatcher()

Try

myFsWatcher.Path = Me.folderToWatch

myFsWatcher.IncludeSubdirectories = True

myFsWatcher.NotifyFilter = (NotifyFilters.Attributes _

Or NotifyFilters.CreationTime _

Or NotifyFilters.DirectoryName _

Or NotifyFilters.FileName _

Or NotifyFilters.LastAccess _

Or NotifyFilters.LastWrite _

Or NotifyFilters.Security _

Or NotifyFilters.Size)

myFsWatcher.EnableRaisingEvents = True

Catch ex As Exception

My.Computer.FileSystem.WriteAllText(“C:\\test1\\log.txt”, ex.Message, True)

End Try

End Sub

Private Sub myFsWatcher_Created(ByVal sender As Object, ByVal e As FileSystemEventArgs) Handles myFsWatcher.Created

envoie(e.FullPath.ToString)

My.Computer.FileSystem.WriteAllText(“C:\\test1\\log.txt”, e.FullPath & ControlChars.CrLf, True)

End Sub

Private Sub myFsWatcher_Changed(ByVal sender As Object, ByVal e As FileSystemEventArgs) Handles myFsWatcher.Changed

envoie(e.FullPath.ToString)

My.Computer.FileSystem.WriteAllText(“C:\\test1\\log.txt”, e.FullPath & ControlChars.CrLf, True)

End Sub

End Class

 

Démarrer le service automatiquement :

https://guillaumedurupt.wordpress.com/2014/08/22/net-demarrer-un-service-automatiquement/

https://guillaumedurupt.wordpress.com/2014/08/21/net-creer-un-installateur-pour-un-service-windows/?preview=true&preview_id=95&preview_nonce=006182e066

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s