可以模仿我自己的配置文件的web.config重启行为?

时间:2015-12-04 12:49:47

标签: c# asp.net .net asp.net-mvc web-config

修改" web.config" ASP.NET(MVC)应用程序的文件the application is automatically recompiled/restarted,强制修改" web.config"读入。

我的问题:

是否可以将这种更改检测行为应用于我自己的配置文件(让我们说" my-config.json")在ASP.NET网站的根目录中?

即。当有人修改" my-config.json"文件,应用程序重新启动。

1 个答案:

答案 0 :(得分:2)

您可以FileSystemWatcher观看文件并检测更改,然后重新启动应用程序或重新加载设置。

也许您不需要重新启动应用程序,只需要重新加载设置

protected void Application_Start()
{
    // Other initializations ...
    // ....

    var watcher = new FileSystemWatcher();
    //Set the folder to watch
    watcher.Path = Server.MapPath("~/Config");
    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite;
    //Set a filter to watch
    watcher.Filter = "*.json";
    watcher.Changed += watcher_Changed;

    // Begin watching.
    watcher.EnableRaisingEvents = true;
}

void watcher_Changed(object sender, FileSystemEventArgs e)
{
    //Restart application here
    //Or Reload your settings
}