2
我寫了下面的代碼。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();
什麼想法?
謝謝你,女巫線後它會提高?在'x.Serialize(fs,m);'之後或'fs.Close();' – MoShe
我不知道Serialize是否會刷新IO緩衝區,但肯定會關閉。然而,使用你的程序是絕對有可能的,我認爲很有可能沒有打印任何東西,因爲觀察者沒有機會發起事件,程序在關閉之後終止。 –
它只是測試應用程序,看它是否引發事件,我只需要確保onChange事件只會在我完成序列化文件後纔會引發。 – MoShe