getting an exception when refreshing the configuration in memory on change to external config file
- by RKP
Hi, I have a windows service which reads the config settings from an external file which is located at a different path than the path to the executable for the windows service. the windows service uses a FileSystemWatcher to monitor the changes to the external config file and when it the config file is changed, it should refresh the settings in memory by reading the updated settings from the config file. but this is where I am getting an exception "ConfigurationErrorsException" and the message is "An error occurred creating the configuration section handler for appSettings: The process cannot access the file 'M:\somefolder\WindowsService1.Config' because it is being used by another process." and the inner exception is actually "IOException" with same message. here is the code. I am not sure what is wrong with the code. Please help.
protected void watcher_Changed(object sender, FileSystemEventArgs e)
{
ConfigurationManager.RefreshSection(ConfigSectionName);
WriteToEventLog(ConfigKeyCheck);
if (FileChanged != null)
FileChanged(this, EventArgs.Empty);
}
private void WriteToEventLog(string key)
{
if (EventLog.SourceExists(ServiceEventSource))
{
EventLog.WriteEntry(ServiceEventSource,
string.Format("key:{0}, value:{1}", key, ConfigurationManager.AppSettings[key]));
}
}