Working with FileSystemWatcher Class

private void ToggleMonitor(object sender, EventArgs e)
{
    // Create a new FileSystemWatcher object.
    FileSystemWatcher fsWatcher = new FileSystemWatcher();
    switch (btnToggleMonitor.Text)
    {
        //Start Monitoring…
        case startMonitoring:
            if (!txtMonitoringPath.Text.Equals(String.Empty))
            {
                fsWatcher.Path = txtMonitoringPath.Text;
                        
                // Set File Type Filter.
                fsWatcher.Filter = "*.*" or  "*.txt"

                // Monitor files and subdirectories.
                fsWatcher.IncludeSubdirectories = true;


                // Monitor all changes specified in the NotifyFilters.
                fsWatcher.NotifyFilter = NotifyFilters.Attributes |
                                            NotifyFilters.CreationTime |
                                            NotifyFilters.DirectoryName |
                                            NotifyFilters.FileName |
                                            NotifyFilters.LastAccess |
                                            NotifyFilters.LastWrite |
                                            NotifyFilters.Security |
                                            NotifyFilters.Size;
                        
                fsWatcher.EnableRaisingEvents = true;
                       
                // Raise Event handlers.
                fsWatcher.Changed += new FileSystemEventHandler(OnChanged);
                fsWatcher.Created += new FileSystemEventHandler(OnCreated);
                fsWatcher.Deleted += new FileSystemEventHandler(OnDeleted);
                fsWatcher.Renamed += new RenamedEventHandler(OnRenamed);
                fsWatcher.Error += new ErrorEventHandler(OnError);

                btnToggleMonitor.Text = stopMonitoring;
                txtMonitoringPath.Enabled = false;
                txtFileType.Enabled = false;
            }
            break;

        // Stop Monitoring…
        case stopMonitoring:
        default:
            fsWatcher.EnableRaisingEvents = false;
            fsWatcher = null;
            btnToggleMonitor.Text = startMonitoring;
            txtMonitoringPath.Enabled = true;
            txtFileType.Enabled = true;
            break;
    }
}






 
// FileSystemWatcher – OnCreated Event Handler
public void OnCreated(object sender, FileSystemEventArgs e)
{
    // Add event details in listbox.
    this.Invoke((MethodInvoker)delegate { textbox.text(e.FullPath, e.ChangeType); });
}

// FileSystemWatcher – OnChanged Event Handler
public void OnChanged(object sender, FileSystemEventArgs e)
{
    //Do something.
}


// FileSystemWatcher – OnRenamed Event Handler
public void OnRenamed(object sender, RenamedEventArgs e)
{
     System.Threading.Thread.Sleep(6000);
     string oldPath = e.OldFullPath;
     string newPath = e.FullPath;
     string oldName = e.OldName;
     string newName = e.Name;
}

// FileSystemWatcher – OnDeleted Event Handler
public void OnDeleted(object sender, FileSystemEventArgs e)
{
//Do something
}

// FileSystemWatcher – OnError Event Handler
public void OnError(object sender, ErrorEventArgs e)
{
e.GetException().Message
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s