簡體   English   中英

如何檢測正在從文件夾中刪除的文件

[英]How to detect a file being removed from folder

我試圖檢測何時從驅動器中的文件夾中刪除文件。 檢測后,我想編寫可以執行某些操作的代碼。 在 C# 中是否有這種“事件”的事件處理程序? 環顧四周,卻沒有找到。 甚至有可能嗎?

您可以使用 FileSystemWatcher 來監視目錄,並訂閱它的Deleted事件。 看下面的代碼

static void Main(string[] args)
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = "C:/some/directory/to/watch";
    watcher.NotifyFilter = NotifyFilters.LastAccess |
                           NotifyFilters.LastWrite  | 
                           NotifyFilters.FileName   | 
                           NotifyFilters.DirectoryName;
    watcher.Filter = "*.*";
    watcher.Deleted += new FileSystemEventHandler(OnDeleted);
    watcher.EnableRaisingEvents = true;
}

private static void OnDeleted(object sender, FileSystemEventArgs e)
{
    throw new NotImplementedException();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM