您好我目前正在尝试测试配置文件更改失败,
我使用以下代码来执行此操作...
string path = _pathInvalidFormat.Substring(0, _pathInvalidFormat.LastIndexOf('\\'));
System.IO.File.Copy("TestInvalidXmlConfigurationFile.xml", _pathInvalidFormat, true);
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(path);
//catch the invalid file getting deleted
fileSystemWatcher.Deleted += new FileSystemEventHandler(fileSystemWatcher_Deleted);
//catch the temporary config file getting renamed
fileSystemWatcher.Renamed += new RenamedEventHandler(fileSystemWatcher_Renamed);
fileSystemWatcher.EnableRaisingEvents = true;
CreateConfig(_pathInvalidFormat);
System.Threading.Thread.Sleep(5000);
Assert.That(_oldFileDeleted);
Assert.That(_newFileCreated);
ResetConfig(_pathInvalidFormat);
但是我对 System.Threading.Thread.Sleep(5000); 的这种使用不满意。
我试过使用 fileSystemWatcher.WaitForChanged(WatcherChangeTypes.Deleted, 5000); 但似乎无法让它工作,因为它似乎总是达到超时。即使我可以看到 Deleted 事件已被击中,它仍然没有返回。
或者有没有更好的方法让系统等待异步事件被触发?
谢谢
基兰
-- 编辑:
我使用 createconfig(path) 使用以下方法创建系统配置文件
private void ReadConfiguration(string path)
{
var configFileMap = new ExeConfigurationFileMap()
{
ExeConfigFilename = path,
};
// if we try to read bad formatted file let's
// 1. delete this file
// 2. call read configuration to form correct file
try
{
Config = System.Configuration.ConfigurationManager
.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
}
catch (ConfigurationErrorsException)
{
// ok, there is bad format file, let delete it and create another one
// TODO: check security rights?
File.Delete(path);
ReadConfiguration(path);
}
}
希望这可以让我了解我的测试到底想要达到什么目的。