FileSystemWatcher没有调用change事件

时间:2012-04-11 15:57:18

标签: c# events xml-serialization filesystemwatcher

我写了以下代码。

 [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();

任何想法?

1 个答案:

答案 0 :(得分:3)

您必须将EnableRaisingEvents设置为true才能开始引发事件。