|
-
Sep 17th, 2009, 01:07 AM
#1
Thread Starter
Member
File change Notification
I have implemented the file system change notifications for specific (.txt) file using following code from MSDN help no action been performed either in the output window or on the form.Can any one help where i am doing wrong.
Imports System.Diagnostics
Imports System.IO
Public Class watcher
Public Shared Sub Main()
Run()
End Sub
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 = "FCN.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(ByVal source As Object, ByVal 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(ByVal source As Object, ByVal 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
-
Sep 17th, 2009, 08:36 AM
#2
Re: File change Notification
This worked fine for me. I had it monitor C:\Temp. When I renamed a file to FCN.txt, it gave a message, and when I saved the file, it also gave messages:
Code:
File: C:\Temp\New Text Document.txt renamed to C:\Temp\fcn.txt
File: C:\Temp\fcn.txt 4
File: C:\Temp\fcn.txt 4
File: C:\Temp\fcn.txt 4
File: C:\Temp\fcn.txt 4
File: C:\Temp\fcn.txt 4
File: C:\Temp\fcn.txt 4
File: C:\Temp\fcn.txt 4
File: C:\Temp\fcn.txt 4
-
Sep 17th, 2009, 10:28 PM
#3
Thread Starter
Member
Re: File change Notification
Where i am doing wrong still not able to traced.which event you had written this code.
-
Sep 17th, 2009, 10:38 PM
#4
Re: File change Notification
I copied your code and ran it and it worked. It only works for files named FNC.txt though.
-
Sep 17th, 2009, 10:48 PM
#5
Thread Starter
Member
Re: File change Notification
Where you are monitoring the out put in Output window or on the form. you need to run the form every time when you make changes to a file.Quite amazing still i am not able to trace it.
-
Sep 17th, 2009, 11:59 PM
#6
Re: File change Notification
if you message box it does it work?
i use a mod'ed version of this ... and my case works
Kris
-
Sep 18th, 2009, 06:49 AM
#7
Re: File change Notification
I was using the console window to monitor the app. I basically took your code, put it in a new console app and ran it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|