Вы находитесь на странице: 1из 4

using using using using using using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; System.IO;

namespace FileSystemWatcherDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) { }

private void fsw_changed(object sender, FileSystemEventArgs e) { switch (e.ChangeType) { case WatcherChangeTypes.Changed: lbxCambios.Items.Add("Archivo Cambiado: " + e.FullPath); break; case WatcherChangeTypes.Created: lbxCambios.Items.Add("Archivo Creado: " + e.FullPath); break; case WatcherChangeTypes.Deleted: lbxCambios.Items.Add("Archivo Eliminado: " + e.FullPath); break; } } private void fsw_renamed(object sender, RenamedEventArgs e) { lbxCambios.Items.Add("Archivo renombrado de " + e.OldName + " a " + e.Name); } private void Form1_Load(object sender, EventArgs e) { //chknotificaciones.Items.Add(NotifyFilters.Attributes, true); //chknotificaciones.Items.Add(NotifyFilters.CreationTime, true); //chknotificaciones.Items.Add(NotifyFilters.DirectoryName, true); //chknotificaciones.Items.Add(NotifyFilters.FileName, true); //chknotificaciones.Items.Add(NotifyFilters.LastAccess, true); //chknotificaciones.Items.Add(NotifyFilters.LastWrite, true); //chknotificaciones.Items.Add(NotifyFilters.Security, true); //chknotificaciones.Items.Add(NotifyFilters.Size, true); } private bool ValidarRuta() { bool bStatus = true; if (txtRuta.Text == "") { errorProvider1.SetError(txtRuta, "Por favor Seleccione una ruta vlida"); bStatus = false; } else { DirectoryInfo dir = new DirectoryInfo(txtRuta.Text); if (dir.Exists) { errorProvider1.SetError(txtRuta, ""); // validar el texto del filtro if (!String.IsNullOrWhiteSpace(txtFiltro.Text)) { errorProvider1.SetError(txtFiltro, ""); }

else { errorProvider1.SetError(txtFiltro, "Por favor Seleccione un filtro vlido"); bStatus = false; } } else { errorProvider1.SetError(txtRuta, "Por favor Seleccione una ruta vlida"); bStatus = false; } } return bStatus; } private void button2_Click(object sender, EventArgs e) { if (ValidarRuta()) { if (btnIniciar.Text == "Iniciar") { btnIniciar.Text = "Detener"; fileSystemWatcher1.Path = txtRuta.Text; fileSystemWatcher1.Changed FileSystemEventHandler(fsw_changed); fileSystemWatcher1.Created FileSystemEventHandler(fsw_changed); fileSystemWatcher1.Deleted FileSystemEventHandler(fsw_changed); fileSystemWatcher1.Renamed RenamedEventHandler(fsw_renamed); += new += new += new += new

if (subSi.Checked == true) { fileSystemWatcher1.IncludeSubdirectories = true; } if (subNo.Checked == true) { fileSystemWatcher1.IncludeSubdirectories = false; } fileSystemWatcher1.Filter = txtFiltro.Text; fileSystemWatcher1.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size; fileSystemWatcher1.EnableRaisingEvents = true; }

else { btnIniciar.Text = "Iniciar"; fileSystemWatcher1.EnableRaisingEvents = false; } } } private void radioButton1_CheckedChanged(object sender, EventArgs e) { } private void button1_Click_1(object sender, EventArgs e) { if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { txtRuta.Text = folderBrowserDialog1.SelectedPath; DirectoryInfo dir = new DirectoryInfo(txtRuta.Text); foreach (FileInfo fi in dir.GetFiles()) { lbxArchivos.Items.Add(fi.Name); } } } private void textBox2_TextChanged(object sender, EventArgs e) { } }

Вам также может понравиться