【发布时间】:2012-02-13 21:14:40
【问题描述】:
我使用 Visual Studio 2010 C# 创建了一个 ASP.NET 网站。
我的程序读取一个配置文件来创建一些类并显示信息。
配置文件未包含在项目中(未出现在解决方案资源管理器中)。如果我在应用程序未运行时修改文件,然后再运行它,它仍会读取旧版本,就像将其保存在缓存中一样。我必须关闭 Visual Studio 才能接受更改。
我的第二个问题与(如果不是由)我的第一个问题有关。我正在使用 FileSystemWatcher 查看配置文件是否在应用程序运行时被修改,但从未调用 Changed 事件。
private string _configFilePath;
private FileSystemWatcher _watcher;
protected void Page_Load(object sender, EventArgs e)
{
//Gets the config file in the application's parent directory
string appPath = this.MapPath("~");
string[] split = appPath.Split('\\');
_configFilePath = appPath.Substring(0, appPath.Length - split[split.Length-1].Length);
Application.Add("watcher", new FileSystemWatcher(_configFilePath.Substring(0, _configFilePath.Length-1), "*.xml"));
_watcher = (FileSystemWatcher)Application["watcher"];
_watcher.NotifyFilter = NotifyFilters.FileName;
_watcher.Changed += new System.IO.FileSystemEventHandler(Watcher_Changed);
_configFilePath += "ProductsConfig.xml";
UpdateDisplay();
}
private void Watcher_Changed(object source, FileSystemEventArgs e)
{
UpdateDisplay();
}
我该如何解决这个问题?
谢谢
【问题讨论】:
标签: c# asp.net xml web filesystemwatcher