Drag FileSystemWatcher control in your form from toolbox and add the events for created,deletion and renaming.
Ex:-
#region Designer view
this.fileSystemWatcher.EnableRaisingEvents = true;
this.fileSystemWatcher.IncludeSubdirectories = true;
this.fileSystemWatcher.SynchronizingObject = this;
this.fileSystemWatcher.Created += new System.IO.FileSystemEventHandler(this.fileSystemWatcher_Created);
this.fileSystemWatcher.Deleted += new System.IO.FileSystemEventHandler(this.fileSystemWatcher_Deleted);
this.fileSystemWatcher.Renamed += new System.IO.RenamedEventHandler(this.fileSystemWatcher_Renamed);
public System.IO.FileSystemWatcher fileSystemWatcher;
#endregion
Create three bool variables to set true or false :-
#region Properties
private bool _fileDeleted = false;
private bool _fileRenamed = false;
private bool _fileCreated = false;
public bool FileDeleted
{
set
{
_fileDeleted = value;
}
get
{
return _fileDeleted;
}
}
public bool FileRenamed
{
set
{
_fileRenamed = value;
}
get
{
return _fileRenamed;
}
}
pblic bool FileCreated
{
set
{
_fileCreated = value;
}
get
{
return _fileCreated;
}
}
#endregion
#region EventHandlers
private void fileSystemWatcher_Renamed(object sender, RenamedEventArgs e)
{
_fileRenamed = true;
MessageBox.Show(e.Name + " Renamed in " + e.FullPath);
}
private void fileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
{
_fileDeleted = true;
MessageBox.Show(e.Name + " Deleted in " + e.FullPath);
}
private void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
_fileCreated = true;
MessageBox.Show(e.Name + " created in " + e.FullPath);
}
#endregion
By this property if you want to check file has been removed ,deleted from physical path then this will be true and you can check like if you have a tree view and you dont want to repopulate your tree view then use filesystemwatcher and repopulate the tree only when files have been changed,delete or rename. or you can throw a messagebox like above.
Ex:-
#region Designer view
this.fileSystemWatcher.EnableRaisingEvents = true;
this.fileSystemWatcher.IncludeSubdirectories = true;
this.fileSystemWatcher.SynchronizingObject = this;
this.fileSystemWatcher.Created += new System.IO.FileSystemEventHandler(this.fileSystemWatcher_Created);
this.fileSystemWatcher.Deleted += new System.IO.FileSystemEventHandler(this.fileSystemWatcher_Deleted);
this.fileSystemWatcher.Renamed += new System.IO.RenamedEventHandler(this.fileSystemWatcher_Renamed);
public System.IO.FileSystemWatcher fileSystemWatcher;
#endregion
Create three bool variables to set true or false :-
#region Properties
private bool _fileDeleted = false;
private bool _fileRenamed = false;
private bool _fileCreated = false;
public bool FileDeleted
{
set
{
_fileDeleted = value;
}
get
{
return _fileDeleted;
}
}
public bool FileRenamed
{
set
{
_fileRenamed = value;
}
get
{
return _fileRenamed;
}
}
pblic bool FileCreated
{
set
{
_fileCreated = value;
}
get
{
return _fileCreated;
}
}
#endregion
#region EventHandlers
private void fileSystemWatcher_Renamed(object sender, RenamedEventArgs e)
{
_fileRenamed = true;
MessageBox.Show(e.Name + " Renamed in " + e.FullPath);
}
private void fileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
{
_fileDeleted = true;
MessageBox.Show(e.Name + " Deleted in " + e.FullPath);
}
private void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
_fileCreated = true;
MessageBox.Show(e.Name + " created in " + e.FullPath);
}
#endregion
By this property if you want to check file has been removed ,deleted from physical path then this will be true and you can check like if you have a tree view and you dont want to repopulate your tree view then use filesystemwatcher and repopulate the tree only when files have been changed,delete or rename. or you can throw a messagebox like above.