Per il controllo della modifica sto ricorrendo al Visual Basic scrivendo il codice con Visual Studio Express 2008.
Mettendo insieme vari pezzi di codice ho fatto questo:
- Codice: Seleziona tutto
Imports System.IO
Module Module1
Sub Main()
' Create a FileSystemWatcher object passing it the folder to watch.
Dim fsw As New FileSystemWatcher("C:\test")
' Assign event procedures to the events to watch.
AddHandler fsw.Changed, AddressOf OnChanged
With fsw
.EnableRaisingEvents = True
.IncludeSubdirectories = False
' Specif the event to watch for.
.WaitForChanged(WatcherChangeTypes.Changed)
' Watch certain file types.
.Filter = "prova.xsl"
' Specify file change notifications.
.NotifyFilter = (NotifyFilters.LastWrite)
End With
End Sub
Public Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
' nasty bug in FileSystemWatcher fires twice (in about 4 ms) on changed file. This is a workaround...
Dim TimeFired = DateTime.Now
If (TimeFired.Subtract(DateTime.Now).TotalMilliseconds < 50) Then
MsgBox("change type: " & e.ChangeType) ' Define message.
End If
End Sub
End Module
Come si vede controllo un solo file XLS in una sola directory precisa. Se faccio partire il programma quando il file XSL è già aperto quando lo si salva partono 4 eventi di "Changed", ma col workaround che ho trovato di queste ne viene notificata solo una perché le altre scattano a 4 millisecondi di distanza. Il problema è un altro (e il workaround non ne è la causa): se apro il file DOPO aver avviato il programmino l'evento scatta subito. Cioè io apro il file XLS e basta, non tocco nulla e già scatta. Nelle proprietà del file la data e l'ora della voce "Modificato" rimane sempre la stessa...
Ma allora come mai si scatena l'handler?