Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How we can make File System Notification (Part 1)

Author: DevASP
Download Source Code : 547_VB.NET - File - How-To File Notifications.zip

In This Article we will discuss How we can create the file System Notification. This topic is divided into two parts and This Article is the Part 1.

The .NET Framework makes it easy to monitor file system changes--the FileSystemWatcher class can raise events when you create, delete, modify, or rename files in the folders that it's "watching". This sample demonstrates the properties exposed by the class, and allows you to interactively try out the various events.To make it easier to configure, each class lives in the same namespace, RemotingSample, which is defined in each class source file. Note, that each class exposes a set of debug properties to help you verify that the object is in fact running from another location (specifically the DebugCodeBase property).

Featured Highlights

The sample form allows you to set properties of the FileSystemWatcher object used by the demonstration, including:

  • Path: The file system path to be "watched" by the object.
  • Filter: The types of files to be watched. By default, this is *.*, although you can change this to any legal file specification.
  • NotifyFilter: The types of changes to watch for. This property can be 0 or more values from the System.IO.NotifyFilters enumeration, combined together. By default, the FileSystemWatcher detects FileName, DirectoryName, and LastWrite changes.
  • IncludeSubdirectories: Indicates whether the monitoring should include directories beneath the selected path.
  • EnableRaisingEvents: If true, the object monitors for file system changes and raises events as they occur. Basically, this is the "on/off switch". In this demonstration, this property is controlled using a CheckBox control displayed as a toggle button (see the Appearance property of the control).

Selecting the Enable Raising Events button turns on event handling for the FileSystemWatcher object, and event handlers in the form's class will write items to the list box on the form as you change files in the selected path. To make it easier to demonstrate, you can use the Create/Rename/Modify/Delete Sample File buttons on the form. These buttons create temporary files in the selected folder. (Use the Delete All Sample Files button to clean up all remaining sample files when you're done.)

You can also use Windows Explorer or any other application to create, modify, rename, and delete files. The sample application will still display the notifications, since the File System Watcher object simply monitors for changes, without regard to the source application. 

Declare The Private Const String. It uses when the code creates temporary files; it will use this name as its root.

Private Const FILE_ROOT As String = "Tempfile.txt"

This function Clear the list of file events.Write this code against Button Clear.

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        lstEvents.Items.Clear()

End Sub

 

Create The New Method against the Create button it Create the file and immediately close it. And Add the new file to the combo box and also Enable and disable buttons.

Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click

        Try

        Dim strFile As String = GetUniqueFileName(fsw.Path, FILE_ROOT)

 

           File.Create(strFile).Close()

 

            cboSampleFile.Items.Add(strFile)

            cboSampleFile.Text = strFile

 

            HandleItems()

 

        Catch exp As Exception

            MessageBox.Show(exp.Message, Me.Text)

        End Try

    End Sub

 

Create The New Method against the Delete button It infact check If the file still exists, delete it, and fix up the combo box to match.

       

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        Dim strName As String = cboSampleFile.Text

 

 

        If CheckFile(strName) Then

            Try

             File.Delete(strName)

                RemoveFileName(strName)

                HandleItems()

 

            Catch exp As Exception

                MessageBox.Show(exp.Message, Me.Text)

            End Try

        End If

    End Sub

  

Create The New Method against the Load Event Of Form. That Function Infact Add the names from the Enum to the CheckedListBox control. You can bind the data source to the return value from the GetValues method, supplying an enumeration type.The GetValues method comes from the System.Enum namespace. It requires an enumeration type, and returns an array of values corresponding to the items in the enumeration. This adds the list of NotifyFilter values to the CheckedListBox control.

Display all the properties of the FileSystemWatcher object.

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        clstNotifyFilter.DataSource = GetValues(GetType(NotifyFilters))

        DisplayFSWProperties(fsw)

End Sub

Create Te New Method against the Rename Button Of Form. If the file still exists, rename it.

Private Sub btnRename_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRename.Click

        Dim strOld As String

        Dim strNew As String

        strOld = cboSampleFile.Text

        If CheckFile(strOld) Then

  strNew = GetUniqueFileName(Path.GetDirectoryName(strOld),              Path.GetFileName(strOld))

            Try

                File.Move(strOld, strNew)

                cboSampleFile.Items.Remove(strOld)

                cboSampleFile.Items.Add(strNew)

                cboSampleFile.Text = strNew

 

            Catch exp As Exception

                MessageBox.Show(exp.Message, Me.Text)

            End Try

        End If

    End Sub

 

Create Te New Method against the cboSampleFile_SelectedIndexChanged.it Check to make sure the selected file is still around. If not, ask to have it removed.

Private Sub cboSampleFile_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSampleFile. SelectedIndexChanged

    CheckFile(cboSampleFile.Text)

        HandleItems()

End Sub

Create Te New Method against the chkEvents_CheckedChanged. It checked Is the FileSystemWatcher component monitoring file changes?

If the events are enabled, disable all the controls for gathering properties. Otherwise, enable the controls.

Private Sub chkEvents_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkEvents.CheckedChanged

        Dim blnIsRunning As Boolean

        If chkEvents.Checked Then

            Try

                GatherFSWProperties(fsw)

            Catch exp As Exception

                MessageBox.Show(exp.Message, Me.Text)

                chkEvents.Checked = False

            End Try

        End If

        blnIsRunning = chkEvents.Checked

        fsw.EnableRaisingEvents = blnIsRunning

 

        txtPath.Enabled = Not blnIsRunning

        txtFilter.Enabled = Not blnIsRunning

        chkIncludeSubdirectories.Enabled = Not blnIsRunning

        clstNotifyFilter.Enabled = Not blnIsRunning

End Sub

I Explain Some of Important functions here Source code is given with that article. Comments are very Important With Code.this is the Part one of How we can make File System Notification.The Other Functions I will  Explain In second Part.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Sending Email Using Visual Basic.Net

Disclaimer - Privacy
© 2002-2017 DevASP.net