首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#怎么监控文件夹被删除

2013-09-12 
C#如何监控文件夹被删除?是文件夹被删除,不是文件夹下的文件。谢谢[解决办法]refer:http://www.codeproject

C#如何监控文件夹被删除?
是文件夹被删除,不是文件夹下的文件。谢谢
[解决办法]
refer:http://www.codeproject.com/Articles/11140/DirectoryWatcher
[解决办法]
判断这个文件夹是否存在? 不存在就被删除了

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

[解决办法]
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;

热点排行