A
Andy B
What would i use to look for changes in a file every minute?
Michel Posseth said:Hello Andy ,
If you want an event driven aproach go for the file system watcher as this
can give you an event when a change took place in a file
so also when there are 2 changes within a minute you get 2 change events .
Ofcourse you could also just use a timer , read the file in a string
variabel and compare the 2 strings if the 2 strings are not the same the
file has changed
however this wil cost you more CPU / IO cycles
Use the filesystem watcher to get notified when a change on the file took
place , and then reread / process the file
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
in the link i provided ( also in your previous post ) there is example
code included
in case you missed it
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()
' 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 = args(1)
' 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)
' Only watch text files.
watcher.Filter = "*.txt"
' Add event handlers.
AddHandler watcher.Changed, AddressOf OnChanged
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)
End Sub
Private Shared Sub OnRenamed(source As Object, e As RenamedEventArgs)
' Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath,
e.FullPath)
End Sub
End Class
Michel Posseth [MCP]
http://www.vbdotnetcoder.com
Andy B said:What would i use to look for changes in a file every minute?
I need to look for things inside the file every minute even if the file
hasn't changed..
Hello Andy ,If you want an event driven aproach go for the file system watcher as this
can give you an event when a change took place in a file
so also when there are 2 changes within a minute you get 2 change events .Ofcourse you could also just use a timer , read the file in a string
variabel and compare the 2 strings if the 2 strings are not the same the
file has changed
however this wil cost you more CPU / IO cyclesUse the filesystem watcher to get notified when a change on the file took
place , and then reread / process the file
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspxin the link i provided ( also in your previous post ) there is example
code included
in case you missed itPublic Class WatcherPublic Shared Sub Main()Run()End Sub<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
Private Shared Sub RunDim args() As String = System.Environment.GetCommandLineArgs()
' 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 = args(1)
' 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)
' Only watch text files.
watcher.Filter = "*.txt"' Add event handlers.
AddHandler watcher.Changed, AddressOf OnChanged
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)
End SubPrivate Shared Sub OnRenamed(source As Object, e As RenamedEventArgs)
' Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath,
e.FullPath)
End SubEnd ClassMichel Posseth [MCP]
http://www.vbdotnetcoder.com
- Show quoted text -
I need to look for things inside the file every minute even if the file
hasn't changed..
Michel Posseth said:Hello Andy ,If you want an event driven aproach go for the file system watcher as this
can give you an event when a change took place in a file
so also when there are 2 changes within a minute you get 2 change events .Ofcourse you could also just use a timer , read the file in a string
variabel and compare the 2 strings if the 2 strings are not the same the
file has changed
however this wil cost you more CPU / IO cyclesUse the filesystem watcher to get notified when a change on the file took
place , and then reread / process the file
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspxin the link i provided ( also in your previous post ) there is example
code included
in case you missed itPublic Class WatcherPublic Shared Sub Main()Run()End Sub<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
Private Shared Sub RunDim args() As String = System.Environment.GetCommandLineArgs()
' 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 = args(1)
' 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)
' Only watch text files.
watcher.Filter = "*.txt"' Add event handlers.
AddHandler watcher.Changed, AddressOf OnChanged
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)
End SubPrivate Shared Sub OnRenamed(source As Object, e As RenamedEventArgs)
' Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath,
e.FullPath)
End SubEnd ClassMichel Posseth [MCP]
http://www.vbdotnetcoder.com
hasn't changed..
Andy B said:I need to look for things inside the file every minute even if the file
hasn't changed..
Michel Posseth said:Hello Andy ,
If you want an event driven aproach go for the file system watcher as
this can give you an event when a change took place in a file
so also when there are 2 changes within a minute you get 2 change events
.
Ofcourse you could also just use a timer , read the file in a string
variabel and compare the 2 strings if the 2 strings are not the same the
file has changed
however this wil cost you more CPU / IO cycles
Use the filesystem watcher to get notified when a change on the file took
place , and then reread / process the file
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
in the link i provided ( also in your previous post ) there is example
code included
in case you missed it
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()
' 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 = args(1)
' 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)
' Only watch text files.
watcher.Filter = "*.txt"
' Add event handlers.
AddHandler watcher.Changed, AddressOf OnChanged
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)
End Sub
Private Shared Sub OnRenamed(source As Object, e As RenamedEventArgs)
' Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath,
e.FullPath)
End Sub
End Class
Michel Posseth [MCP]
http://www.vbdotnetcoder.com
Andy B said:What would i use to look for changes in a file every minute?