c# - FileSystemWatcher 未调用更改事件

标签 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();

有什么想法吗?

最佳答案

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

关于c# - FileSystemWatcher 未调用更改事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10109582/

相关文章:

c# - 如何将 KeyValuePair.Value 与我指定的值进行比较?

c# - ADO .NET EF 中的可组合查询每次迭代返回更多实体

c# - Linq IN 运算符

javascript - 为什么 onmouseover 有效,但 onkeydown 无效?

.net - 命名空间未通过 DataContractSerializer 从 XML 根中删除

.net - 为什么 .NET 中没有 XML 可序列化字典?

c# - 具有派生类型的 XML 序列化

c# - 如何在自定义 WPF 控件中呈现动态数据,例如折线图?

javascript - 将 select 的内容拆分到不同的字段中

c# - 复选框 CheckedChanged 事件