开发者

windows service - config file

I know this has probably been asked before but I can't seem to find the right answer for me.

I have a windows service named foobar.exe. I have an application configuration file named foobar.exe.config in the same folder.

Is the config file only read at startup?

I would like to make changes 开发者_JAVA百科to the config file without having to restart the service but that is the only way I can get the new settings read.

What am I doing wrong?

Can a windows service have a dynamic config file?


.NET applications will read their config files at startup and cache them for performance reasons.

I see two basic ways around this:

  • in your service, keep track of the last update date/time for the config file. Check this last update date regularly and if you detect a change, re-load the config file

  • a Windows NT service can respond to a special OnCustomCommand event. You could implement such an event handler to reload the config, and when you do change the config, you could have a small utility to signal to your service that the config has changed and send that "custom command" to your service


Assuming your windows service was written with .NET:

Configuration files are only read at startup. If you change values in the configuration, you will need to restart the service in order for them to be picked up.

If you want to have dynamic configuration, you will need to implement this yourself - polling the filesystem to see if the configuration file changed and apply the changes.


You may need to look at using FileSystemWatcher. See pseudo-c#-code example below.

private FileSystemWatcher _myWatcher;

protected override void OnStart(string[] args)
{
_myWatcher = new FileSystemWatcher();
_myWatcher.Path = path to config file;
_myWatcher.Changed += new FileSystemEventHandler(myWatcherFileChanged);
_myWatcher.NotifyFilter = NotifyFilters.LastWrite;
_myWatcher.EnableRaisingEvents = true;

}

protected override void OnStop()
{
    _myWatcher.EnableRaisingEvents = false;
}

private void myWatcherFileChanged(object sender, FileSystemEventArgs e)
{
...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜