ARTICLE

FileSystemWatcher Control in VB.NET

Posted by Shalini Juneja Articles | ASP.NET using VB.NET March 25, 2011
In this article we demonstrate on what is FileSystemWatcher control and how can you make a folder monitor application using FileSystemWatcher control
Download Files:
 
Reader Level:

A visual basic FileSystemWatcher control is used to watch for changes in a specified directory. You can watch for changes in the Files and subdirectories of the specified directory. You can create a component to watch files on a local computer, A network drive or a remote computer. The Filter property of the FileSystemWatcher is used to watch for changes in all the Files. You can also watch for renaming, deletion or creation of files or directories. It sopports several types of changes in a directory or file. In given example changes in Attribute, the Last write Date and Time, or the size of the files or directories. You can do this by setting NotifyFilter property to one of the NotifyFilter Value.
Here we are taking an example that how you can monitor a folder using FileSystemWatcher control. This application monitor specific folder tells you when you create a new file or delete any file that What's happened.

  • simply just open a new project 

  • Drag one TextBox, one button, two ListView, FolderBrowserDialog and a FileSystemWatcher control on form. The form will look like below.
     
    FileSystemWatcher1.gif

    FileSystemWatcher1.1.gif

  • Write the given code.

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Text = "shalini"
            Me.BackColor = Color.Ivory
            ListView1.BackColor = Color.LightGray
            ListView2.BackColor = Color.LightGray
            ListView1.ForeColor = Color.Brown
            ListView2.ForeColor = Color.Red
            FileSystemWatcher1.Path = "c:\"
        End Sub
        Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles
           
    FileSystemWatcher1.Created
            ListView1.Items.Add(e.Name)
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            FolderBrowserDialog1.ShowDialog()
            inp.Text = FolderBrowserDialog1.SelectedPath
            FileSystemWatcher1.Path = inp.Text
        End Sub
        Private Sub FileSystemWatcher1_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles
           
    FileSystemWatcher1.Deleted
            ListView2.Items.Add(e.Name)
        End
    Sub

    Output:-

    FileSystemWatcher2.gifFileSystemWatcher4.gif

    FileSystemWatcher3.gifFileSystemWatcher3.1.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor