如何解决停止 FileSystemWatcher 后运行另一段代码
我在 C# 中有一个 FileSystemWatcher
代码,它检测已更改其内容的文件,然后将这些文件复制到指定目录 (C:\Users\wost\Desktop\Data\files)。复制文件后,在 FileSystemWatcher
停止后,我想加密这些文件,我想在同一个程序中执行此操作。
所以我在主程序中有以下代码:
Watcher w = new Watcher(lexpt,fexpt,userName);
w.Watch();
// Function which encrypts the files
Encryption.EncryptFiles(@"C:\Users\wost\Desktop\Data\files");
这不起作用,因为 FileSystemWatcher
永远不会停止,只有当我退出程序时。因此,我决定使用如下代码所示的全局变量 Global.serviceRunning
,它阻止 FileSystemWatcher
在指定目录中观看:
public void Watch()
{
FileSystemWatcher watcher;
using (watcher = new FileSystemWatcher(
$"C:\\Users\\wost\\AppData\\Roaming\\Sublime Text 3",_ext))
{
// Watch for changes in LastAccess and LastWrite times,and
// the renaming of files or directories.
watcher.NotifyFilter = NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName;
// Only watch text files.
// watcher.Filter = "*.txt";
watcher.IncludeSubdirectories = true;
// Add event handlers.
watcher.Changed += OnChanged;
watcher.Created += OnChanged;
watcher.Deleted += OnChanged;
watcher.Renamed += OnRenamed;
// Begin watching.
watcher.EnableRaisingEvents = true;
//GLOBAL VARIABLE
while (Global.serviceRunning == true) ;
}
}
我曾尝试在主程序中将此全局变量的值更改为 false
,但它不起作用。你能帮我找到一种方法来停止FileSystemWatcher
,然后加密给定目录中的文件吗?
解决方法
您可以尝试取消相关事件以停止 FileSystemWatcher。
代码:
static void Stop()
{
watcher.EnableRaisingEvents = false;
watcher.Changed -=new FileSystemEventHandler(OnChanged);
watcher.Created -= new FileSystemEventHandler(OnChanged);
watcher.Deleted -= new FileSystemEventHandler(OnChanged);
watcher.Renamed -= new RenamedEventHandler(OnRenamed);
watcher.Dispose();
}
我在控制台应用中进行了测试。
完整代码:
class Program
{
static FileSystemWatcher watcher= new FileSystemWatcher();
public static void Main(string[] args)
{
watcher.Path = "E:\\Example";
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite |
NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.txt";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.EnableRaisingEvents = true;
Console.WriteLine("Press\'q\' to stop the watcher.");
while (Console.ReadLine()=="q")
{
Stop();
};
Console.ReadKey();
}
static void Stop()
{
watcher.EnableRaisingEvents = false;
watcher.Changed -=new FileSystemEventHandler(OnChanged);
watcher.Created -= new FileSystemEventHandler(OnChanged);
watcher.Deleted -= new FileSystemEventHandler(OnChanged);
watcher.Renamed -= new RenamedEventHandler(OnRenamed);
watcher.Dispose();
}
public static void OnChanged(object sender,FileSystemEventArgs e)
{
Console.WriteLine("file:" + e.FullPath + "" + e.ChangeType);
}
public static void OnRenamed(object sender,RenamedEventArgs e)
{
Console.WriteLine("Fi]e:{0} renamed to{1}",e.OldFullPath,e.FullPath);
}
}
,
正如其他人指出的 in the comments,FileSystemWatcher.EnableRaisingEvents
属性是您应该使用的机制,用于激活或停用 FileSystemWatcher
组件对目标目录的监视。
请注意,尽管 FileSystemWatcher
事件是在 ThreadPool
上引发的,但内部机制使得在组件停用后无法调用处理程序。但是,当您将 EnableRaisingEvents
设置为 false
时,不会中止任何已经运行的处理程序。因此,您应该考虑到,在监控停用之后,某些文件可能会在后台继续被复制。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。