FileSystemWatcher 未调用更改事件
本文关键字:事件 调用 FileSystemWatcher | 更新日期: 2023-09-27 18:30:41
我写了下面的代码。
[XmlRoot("myxml")]
public class myxml
{
[XmlElement("one")]
public string one { get; set; }
[XmlElement("two")]
public string two { get; set; }
}
class Program
{
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
//
static void Main(string[] args)
{
myxml m = new myxml();
m.one = "111";
m.two = "222";
FileSystemWatcher watcher = new FileSystemWatcher(@"c:'", "myxml.xml");
watcher.Changed += new FileSystemEventHandler(OnChanged);
FileStream fs = new FileStream(@"c:'myxml.xml", FileMode.Open);
XmlSerializer x = new XmlSerializer(m.GetType());
x.Serialize(fs, m);
fs.Close();
}
}
现在我认为在以下行之后将调用 OnChanged 事件,但没有......
x.Serialize(fs, m);
同样在这行之后什么也没发生
fs.Close();
知道吗?
您必须将 EnableRaisingEvents 设置为 true 才能开始引发事件。