I have a WPF program that Watchs a folder and I want to write on a listbox every event that occurs.
My problem is that i have a class FileWatcher and I can't pass the events (Created, Changed and deleted) to the listbox.
Can someone help please?
Thanks
MainWindow
public MainWindow()
{
InitializeComponent();
string Location = string.Empty; //variavel que vai definir a localização da monitorização
}
private void btMon_Click(object sender, RoutedEventArgs e)
{
FolderBrowserDialog browseFolder = new FolderBrowserDialog();
browseFolder.Description = "Selecione a pasta que deseja monitorizar. Todas as subpastas serão monitorizadas.";
if (browseFolder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Location = browseFolder.SelectedPath;
System.Windows.MessageBox.Show(Location);
}
} // Definição da localização que passa para o Filewatcher
private void btInMon_Click(object sender, RoutedEventArgs e)
{
monitoriza = new FileWatcher(Location);
} // Inicio da monitorização com a class FileWatcher
Class FileWatcher
public class FileWatcher
{
public MainWindow main;
private FileSystemWatcher _fileWatcher;
public FileWatcher(string Location)
{
_fileWatcher = new FileSystemWatcher(PathLocation(Location));
_fileWatcher.IncludeSubdirectories = true;
_fileWatcher.Created += new FileSystemEventHandler(_fileWatcher_Created);
_fileWatcher.EnableRaisingEvents = true;
}
private string PathLocation(string Location)
{
string value = String.Empty;
try
{
value = Location;
if (value != string.Empty)
{
return value;
}
}
catch (Exception ex)
{
//Implement logging on future version
}
return value;
}
void _fileWatcher_Created(object sender, FileSystemEventArgs e)
{
Logging.Log(String.Format("Evento criado por " + Environment.UserName + " Em " + DateTime.Now + " File Created: Patch:{0}, Name:{1}", e.FullPath, e.Name));
}
}