文件使用的例外所以我这样做:获取使用FileSystemWatcher的
public MainWindow()
{
InitializeComponent();
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = "F:\\Scoreboard Assistant\\output\\";
watcher.Filter = "event.xml";
watcher.NotifyFilter=NotifyFilters.LastWrite;
watcher.Changed += new FileSystemEventHandler(file_Changed);
watcher.EnableRaisingEvents = true;
}
private void file_Changed(object sender, FileSystemEventArgs e)
{
XmlDocument config = new XmlDocument();
config.Load(e.FullPath.ToString());
string text1 = config.SelectSingleNode("event/text1").InnerText;
string text2 = config.SelectSingleNode("event/text2").InnerText;
}
什么我做的是看更改到一个特定的XML文件。然后,如果检测到对文件的更改,它将读取XML文件并从中提取变量。但是,当我运行代码时,出现以下错误:
An unhandled exception of type 'System.IO.IOException' occurred in System.Xml.dll
Additional information: The process cannot access the file 'F:\Scoreboard Assistant\output\event.xml' because it is being used by another process.
如何解决此问题?
谢谢。我没有意识到改变的事件会被解雇这么多次。把我的代码放入一个空的try-catch中修复它。 – 2014-10-18 22:00:32