FileSystemWatcher对象在C#

FileSystemWatcher对象在C#

本文介绍了Windows服务瓦特/ FileSystemWatcher对象在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我要创建一个程序,监视文件大小的变化。我已经做了一个简单的Windows服务,FileSystemWatcher的,所以我现在很熟悉瓦特/概念。我还做了检查文件大小(在表格的按钮,使它),但并没有在我的FileSystemWatcher的尚未执行代码。我如何创建具有监视文件的大小filewatcher一个窗口服务?我必须把FileSystemWatcher的窗口服务中,并通过OnStart方法调用观察者?

I have to create a program that monitors changes in file size. I already made a simple windows service and filesystemwatcher so I am now familiar w/ the concept. I also made a code that checks for the filesize (made it in a form button)but haven't yet implemented in my filesystemwatcher. How do I create a windows service that has a filewatcher that monitors the file size? Do I have to put a filesystemwatcher inside the windows service and call the watcher via the OnStart method?

推荐答案

如果你正在做一个窗口的服务,那么你会希望以编程方式做到这一点。我通常把形式出我的服务,并为他们进行沟通单独的接口。现在FileSystemWatcher的没有一个事件,仅仅观察大小,所以你要作出这样的联系,FileSystemWatcher.Changed检查修改现有文件的方法。声明并在你的OnStart方法初始化控制和领带的事件一起为好。不要在你的调用OnStop方法的任何清理代码。它应该是这个样子:

If you're making a Window's service, then you'll want to do it programmatically. I usually keep forms out of my services and make a separate interface for them to communicate. Now the FileSystemWatcher doesn't have an event to watch solely for size, so you'll want to make a method that ties to FileSystemWatcher.Changed to check for modifications to existing files. Declare and initialize the control in your OnStart method and tie together the events as well. Do any cleanup code in your OnStop method. It should look something like this:

protected override void OnStart(string[] args)
{
FileSystemWatcher Watcher = new FileSystemWatcher("PATH HERE");
Watcher.EnableRaisingEvents = true;
Watcher.Changed += new FileSystemEventHandler(Watcher_Changed);
}

// This event is raised when a file is changed
private void Watcher_Changed(object sender, FileSystemEventArgs e)
{
// your code here
}

另外请注意,FileSystemWatcher对象将火过多个事件为单个文件,所以当你正在调试手表的模式来解决它。

Also note, the FileSystemWatcher will fire off multiple events for a single file, so when you're debugging watch for patterns to work around it.

这篇关于Windows服务瓦特/ FileSystemWatcher对象在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:15