我正在尝试编写一个程序,该程序将监视目录/磁盘并将已删除的文件/文件夹从网络共享文件夹中删除时移动到回收站。
我有这段代码在删除文件/文件夹时有效,但我需要取消删除并将文件移动到回收站。我在这里找到了如何将文件/文件夹移动到回收站:如何将文件移动到回收站
FileSystemWatcher fsw = new FileSystemWatcher();
private void frmMain_Load(object sender, EventArgs e)
{
StartWatcher("X:\\Virtuals\\");
}
private void StopWatcher()
{
if (fsw != null)
try
{
fsw.EnableRaisingEvents = false;
fsw.Dispose();
fsw = null;
}
catch { }
}
private void StartWatcher(string path)
{
StopWatcher();
FileSystemWatcher fsw = new FileSystemWatcher();
fsw.Path = Path.GetDirectoryName(path);
fsw.Filter = Path.GetFileName(path);
fsw.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite | NotifyFilters.FileName
| NotifyFilters.DirectoryName;
fsw.Deleted += new FileSystemEventHandler(OnDeleted);
fsw.EnableRaisingEvents = true;
}
private void OnDeleted(object source, FileSystemEventArgs e)
{ MessageBox.Show("File deleted: " + e.FullPath); }