Results 1 to 2 of 2

Thread: [RESOLVED] Filesystem Notification

  1. #1

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Resolved [RESOLVED] Filesystem Notification

    Hi, I have an explorer-like interface which in many ways mimiques the behaviour of a standard explorer window. What I need is to find some way of knowing if something had changed in the filesystem since the last time I checked.
    What I mean, for example if a user creates a directory or renames a file - I need a way to know that. Right now I re-read the contents of the displayed folder once per second and I added a manual 'Refresh' button, but I wonder if there is a better solution.
    I tried to use FileSystemWatcher component for that but either I couldn't figure out how it works or it doesn't have such functionality.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Filesystem Notification

    If you want to monitor a folder/file then FileSystemWatcher is the most appropriate class to use.
    I assume you forgot to set EnableRaisingEvents = True due to which you are not getting the events.

    vb.net Code:
    1. Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     '' set EnableRaisingEvents to True so that events are raised when something changes.
    3.     FileSystemWatcher1.EnableRaisingEvents = True
    4.  
    5.     '' the folder we want to monitor for example.
    6.     FileSystemWatcher1.Path = "C:\temp"
    7. End Sub
    8.  
    9. Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
    10.     'this event is fired when file contents is changed etc.
    11. End Sub
    12.  
    13. Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
    14.     'this event is fired when file/folder is created.
    15. End Sub
    16.  
    17. Private Sub FileSystemWatcher1_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Deleted
    18.     'this event is fired when file/folder is deleted.
    19. End Sub
    20.  
    21. Private Sub FileSystemWatcher1_Renamed(ByVal sender As Object, ByVal e As System.IO.RenamedEventArgs) Handles FileSystemWatcher1.Renamed
    22.     'this event is fired when file/folder is renamed.
    23. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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