1
我需要在不同的时间观看几个文件,有时需要同时观看。filewatcher观看几个文件,但以不同的方式处理它们
我用这作为一个测试:
namespace FilewatcherTest
{
public partial class Form1 : Form
{
private System.IO.FileSystemWatcher FSWatcherTest;
public Form1()
{
InitializeComponent();
FSWatcherTest = new FileSystemWatcher();
EventHandling();
FSWatcherTest.Path = @"d:\tmp";
FSWatcherTest.Filter = "file.txt";
// Begin watching.
FSWatcherTest.EnableRaisingEvents = true;
}
protected void EventHandling()
{
FSWatcherTest.Changed += FSWatcherTest_Changed;
FSWatcherTest.Deleted += FSWatcherTest_Deleted;
FSWatcherTest.Renamed += FSWatcherTest_Renamed;
FSWatcherTest.Created += FSWatcherTest_Created;
}
private void FSWatcherTest_Changed(object sender, System.IO.FileSystemEventArgs e)
{
WriteToLog("File Changed");
}
private void FSWatcherTest_Created(object sender, System.IO.FileSystemEventArgs e)
{
WriteToLog("File Created");
}
private void FSWatcherTest_Deleted(object sender, System.IO.FileSystemEventArgs e)
{
WriteToLog("File Deleted");
}
private void FSWatcherTest_Renamed(object sender, System.IO.RenamedEventArgs e)
{
WriteToLog("File Renamed");
}
private void WriteToLog(string message)
{
using (var sw = new StreamWriter(@"d:\tmp\service.log", true))
{
sw.WriteLine(string.Format("{0} {1}", DateTime.Now,message));
}
}
}
}
我当然会,因为这是进入我创建了一个服务改变的硬编码路径一旦我有什么到位。
我的问题是,我可以使用相同的文件观察器,还是应该为每个文件使用唯一的文件?
如果我使用同一个,我怎么知道哪个文件引发了这个事件?
谢谢!
编辑 对不起,我没有使用filesystemwatcher之前,并不知道它是重要的,但文件将在不同的目录中,而不是相同的文件类型。
文件将完全和在不同的位置有不同的名字,我将如何逐个添加,因为我需要? – ErocM 2013-02-17 16:58:45
这取决于你。他们是否有相同的后缀例如?您也可以选择观看整个目录,并且只在触发事件的文件是您的文件之一时才执行某些操作 – Blachshma 2013-02-17 17:01:39
sry在您回答之前,我编辑过它们,它们将位于不同目录中 – ErocM 2013-02-17 17:02:17