Unable to locate app.config file
-
Wednesday, May 09, 2012 10:05 AM
I am trying to use a app.config file in windows service. The following is my code:
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Text; using System.IO; using System.Configuration; namespace MonitorBPSService { public partial class MonitorBPSService : ServiceBase { public MonitorBPSService() { InitializeComponent(); } protected override void OnStart(string[] args) { InitializeConfig(); FileSystemWatcher watcher = new FileSystemWatcher(); NameValueCollection AllAppSettings = ConfigurationManager.AppSettings; watcher.Filter = AllAppSettings["FileToMonitor"]; watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Attributes | NotifyFilters.Size; watcher.Changed += new FileSystemEventHandler(watcher_Changed); watcher.EnableRaisingEvents = true; watcher.Path = AllAppSettings["PathToMonitor"]; } protected override void OnStop() { } protected static void watcher_Changed(Object sender, FileSystemEventArgs e) { } private void InitializeConfig() { ExeConfigurationFileMap myMap = new ExeConfigurationFileMap(); String exeFileName = @"MonitorBPSService.exe.config"; Debug.Assert(File.Exists(exeFileName), "The mapped file or path is missing or incorrect!"); myMap.ExeConfigFilename = exeFileName; ConfigurationManager.OpenMappedExeConfiguration(myMap, ConfigurationUserLevel.None); } } }
For some reason, the assert statement: "The mapped file or path is missing or incorrect!" always got triggered everytime I tried to run the service. I have verified that the MonitorBPSService.exe.config file do exists and is located in the same folder as the exe file. Any idea what am I missing here? Thanks in advance.- Edited by Kel Regor Wednesday, May 09, 2012 10:07 AM
All Replies
-
Wednesday, May 09, 2012 10:14 AM
Hi,
try this:
String exeFileName = AppDomain.CurrentDomain.BaseDirectory + @"MonitorBPSService.exe.config";
Bilhan silva
-
Wednesday, May 09, 2012 10:17 AM
Is that once the service is installed? Check the directory for wherever you've installed it and see if MonitorBPSService.exe.config has been installed.
If it's not there I expect it's not getting deployed properly when the solution is installed. By default I think the properties for "Build Action" = None and "Copy to Output Directory" = Do not copy. I think setting the config file build action to Embedded resource will help. If it's when you debug that the service doesn't work try setting "Copy to Output Directory" = Copy if newer.
-
Friday, May 11, 2012 2:43 AM
Hi Bilhan,
I tried that and it is still giving me the same error.
Regards,
Kel
-
Friday, May 11, 2012 2:47 AM
Hi Nick,
Yes, the MonitorBPSService.exe.config was installed and can be found in the same installation folder as the service.
Regards,
Kel
-
Friday, May 11, 2012 3:08 AM
-
Friday, May 11, 2012 5:54 AM
Hi Darnold,
Thanks for your reply, will try it out. Anyway, I have built another console application version to test the code above. As I stepped through the breakpoints, I noticed that the exception was from this line:
watcher.EnableRaisingEvents = true;
The exception thrown was:System.ArgumentException: The path is not of a legal form.
at System.IO.Path.NormalizePathFast(String path, Boolean fullCheck)
at System.IO.Path.NormalizePath(String path, Boolean fullCheck)
at System.IO.Path.GetFullPathInternal(String path)
at System.IO.Path.GetFullPath(String path)
at System.IO.FileSystemWatcher.StartRaisingEvents()
at System.IO.FileSystemWatcher.set_EnableRaisingEvents(Boolean value)
at TestConsole.Program.Main(String[] args) in D:\MyProgram\MonitorBPSService\TestConsole\Program.cs:line 26The followng are the values from the app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key ="PathToMonitor" value="C:\\Documentum\\logs"/>
<add key ="FileToMonitor" value="BPS.log"/>
</appSettings>
</configuration>Once again, the full code for the test console is as follows:
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Configuration; using System.Collections.Specialized; using System.Diagnostics; namespace TestConsole { class Program { static void Main(string[] args) { try { InitializeConfig(); //Configuration cs = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); FileSystemWatcher watcher = new FileSystemWatcher(); NameValueCollection AllAppSettings = ConfigurationManager.AppSettings; watcher.Filter = AllAppSettings["FileToMonitor"]; watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Attributes | NotifyFilters.Size; watcher.Changed += new FileSystemEventHandler(watcher_Changed); watcher.EnableRaisingEvents = true; watcher.Path = AllAppSettings["PathToMonitor"]; } catch (Exception e) { Console.WriteLine(e.ToString()); } } private static void InitializeConfig() { ExeConfigurationFileMap myMap = new ExeConfigurationFileMap(); String exeFileName = AppDomain.CurrentDomain.BaseDirectory + @"TestConsole.exe.config"; Debug.Assert(File.Exists(exeFileName), "The mapped file or path is missing or incorrect!"); myMap.ExeConfigFilename = exeFileName; ConfigurationManager.OpenMappedExeConfiguration(myMap, ConfigurationUserLevel.None); } protected static void watcher_Changed(Object sender, FileSystemEventArgs e) { } } }
Any idea what went wrong? Thanks again.
Regards,
Kel- Edited by Kel Regor Friday, May 11, 2012 5:54 AM
-
Friday, May 11, 2012 6:02 AM
Hi,
you're setting EnableRisingEvents to true before you set the actual path to monitor, this cause the FileSystemWatcher to start without a path to monito. Just move the EnableRisingEvents = true below the Path = etc
watcher.Path = AllAppSettings["PathToMonitor"]; watcher.EnableRaisingEvents = true;
Bilhan silva
- Marked As Answer by Kel Regor Friday, May 11, 2012 6:30 AM
-
Friday, May 11, 2012 6:17 AM
Hi Kel Regor,
I found the Issue, the problem is you are calling watcher.EnableRaisingEvents = true; line before you setting the path so system doesn't found any paths to monitor when enabling the events.
Please find the modified code and its working as i tested locally...
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Configuration; using System.Collections.Specialized; using System.Diagnostics; namespace TestConsole { class Program { static void Main(string[] args) { try { InitializeConfig(); //Configuration cs = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); FileSystemWatcher watcher = new FileSystemWatcher(); NameValueCollection AllAppSettings = ConfigurationManager.AppSettings; watcher.Filter = AllAppSettings["FileToMonitor"]; watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Attributes | NotifyFilters.Size; watcher.Changed += new FileSystemEventHandler(watcher_Changed); watcher.Path = AllAppSettings["PathToMonitor"]; watcher.EnableRaisingEvents = true; } catch (Exception e) { Console.WriteLine(e.ToString()); } } private static void InitializeConfig() { ExeConfigurationFileMap myMap = new ExeConfigurationFileMap(); String exeFileName = AppDomain.CurrentDomain.BaseDirectory + @"ConsoleApplication1.exe.config"; Debug.Assert(File.Exists(exeFileName), "The mapped file or path is missing or incorrect!"); myMap.ExeConfigFilename = exeFileName; ConfigurationManager.OpenMappedExeConfiguration(myMap, ConfigurationUserLevel.None); } protected static void watcher_Changed(Object sender, FileSystemEventArgs e) { } } }
Hope this will help you ... :)
Sai Kumar K http://www.santoshtechnologies.com http://saimaterial.wordpress.com
- Marked As Answer by Kel Regor Friday, May 11, 2012 6:30 AM

