2013年9月12日星期四

C # how to monitor the folder is deleted ?

Is the folder is deleted, not the file under the folder . Thank
------ Solution --------------------------------------- -----
refer: http://www.codeproject.com/Articles/11140/DirectoryWatcher
- ----- Solution --------------------------------------------
judge this folder exists ? was deleted does not exist

public static bool IsExistsFilePath(string folder)
{
    if (!System.IO.Directory.Exists(folder))
    {
        return false;
    }
    return true;
}

------ Solution ------------------------------------- -------
string path = @"C:\Users\MyFolder\FileWatch"; // watch for parent directory
if (!Directory.Exists(path)) // verify it exists before start
    return;

FileSystemWatcher watcher = new FileSystemWatcher(path);
// set option to track directories only
watcher.NotifyFilter = NotifyFilters.DirectoryName;

watcher.Deleted += (o, e) =>
{
    if (e.FullPath == @"C:\Users\MyFolder\FileWatch\Test")
    {
        // If you are here, your test directory was deleted
    }
};

watcher.EnableRaisingEvents = true;

没有评论:

发表评论