Scheduled Console App Crashes with App.config
I have a C# console application that reads app settings from its
app.config. The console application is targeted as the action of a Windows
Scheduler Task. It runs once a day.
Since, adding the code to read from the configuration file the app crashes
only when run by the Task Scheduler. When run manually (from command
prompt or by clicking on it in it's folder)), the application runs with no
issues and it works exactly as expected.
Here's the code that reads from the app settings section:
int someValue = 1;
try
{
if (ConfigurationManager.AppSettings["someValue"] != null)
someValue =
int.Parse(ConfigurationManager.AppSettings["someValue"].ToString());
}
catch(Exception exception)
{
// Write to error log
}
This is the exception I'm getting in my error log:
Exception: The type initializer for '<my application name>' threw an
exception.
System.Configuration.ConfigurationErrorsException: An error occurred
loading a configuration file: The parameter 'exePath' is invalid.
Parameter name: exePath ---> System.ArgumentException: The parameter
'exePath' is invalid.
Parameter name: exePath
I have tried allowing the task to run with highest privileges. I have
tried accessing the configuration file in different ways
(Configuration.OpenConfigurationExe(...)), etc... I get the same problem
every time. I have tried putting my .exe in various locations on disk.
This one is in C:\Program Files. I have looked around on S.O. and this
post suggests that it might be a read permissions issue, but if the task
runs under my domain's account and has the highest privileges, is that
still possible? Note that it runs just fine if I run it manually from a
command prompt or by clicking on it.
How can I reliably get a console application to read its app.config if it
has been configured to run under Windows Task Scheduler?
No comments:
Post a Comment