how can i set it so that once the content of a certain file (.txt) gets changed, an action gets executed
Printable View
how can i set it so that once the content of a certain file (.txt) gets changed, an action gets executed
FileSystemWatcher
Check out this. Probably the best way. Raises events when a file changes. Just put the code that you want to execute in the Changed event.
ugh i hate the msdn library, i don't quite understand what they are saying...
It's not too bad.
Simply create an instance of the FileSystemWatcher on a Form Load event for example:
VB Code:
Dim mywatcher As New IO.FileSystemWatcher 'Create new instance of FileSystemWatcher mywatcher.Path = "D:\My Documents\" 'Specify Path to File or Directory. Which ever you need mywatcher.NotifyFilter = IO.NotifyFilters.LastWrite Or IO.NotifyFilters.LastAccess Or IO.NotifyFilters.FileName AddHandler mywatcher.Changed, AddressOf OnChanged AddHandler mywatcher.Created, AddressOf OnChanged 'Add handlers to catch certain events of certain types AddHandler mywatcher.Deleted, AddressOf OnChanged 'Use intellisense to see what others are open to you mywatcher.EnableRaisingEvents = True 'Start the process
This is the code operates/fires when an event is detected. You would probably place your code that you want to fire when a file changes.
VB Code:
Private Shared Sub OnChanged(ByVal source As Object, ByVal e As IO.FileSystemEventArgs) ' Specify what is done when a file is changed, created, or deleted. 'This is the event that detects a file change. 'Put your code in here MessageBox.Show("File: " & e.FullPath & " " & e.ChangeType) End Sub
gee thanks a bunch
i changed the "Private Shared Sub" to "Private Sub" as i don't want it throughout each form...
thanks.