How To Use FileSystemWatcher Control in ASP.NET Using VB.NET

In this article we demonstrate on what is FileSystemWatcher control and how can you make a folder monitor application using FileSystemWatcher control.
  • 5100
 

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 supports 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 List View, 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.gif

    FileSystemWatcher4.gif

    FileSystemWatcher3.gifFileSystemWatcher3.1.gif
     

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.