Results 1 to 7 of 7

Thread: File change Notification

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2009
    Posts
    54

    Smile 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

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    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

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2009
    Posts
    54

    Re: File change Notification

    Where i am doing wrong still not able to traced.which event you had written this code.

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: File change Notification

    I copied your code and ran it and it worked. It only works for files named FNC.txt though.

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2009
    Posts
    54

    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.

  6. #6
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    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

  7. #7
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    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
  •  



Click Here to Expand Forum to Full Width