我想使用FileSystemWatcher监视某些目录,但是FileSystemWatcher.created根本不起作用。
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Configuration;
using System.Threading;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static string fileName;
static string dirFileName;
static void Main(string[] args)
{
string _Dir = ConfigurationManager.AppSettings["Dir"];
Console.WriteLine("MAIDProcessFileTest processing started");
FileSystemWatcher _watch = new FileSystemWatcher();
_watch.Path = _Dir;
_watch.Created += FileCreated;
Console.ReadLine();
}
private static void FileCreated(object sender, FileSystemEventArgs e)
{
Console.WriteLine("Before is treat");
fileName = Path.GetFileName(e.FullPath);
dirFileName = Path.GetDirectoryName(e.FullPath) + @"\" + Path.GetFileName(e.FullPath);
}
}
}
有人能告诉我为什么这段代码无法正常工作吗……每当我调试_watch.Created + = FileCreated时;根本不工作。谁能告诉我错误在哪里。请帮助
答案 0 :(得分:3)
您需要启动它
_watch.EnableRaisingEvents = true;
FileSystemWatcher.EnableRaisingEvents Property
您可能应该从这里开始
它有一个很好的例子,使用它需要了解的一切