Aaron
Aaron

Reputation: 7145

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?

Upvotes: 0

Views: 2448

Answers (2)

Aaron
Aaron

Reputation: 7145

Solved it. Turns out I had some legacy bits running code like this:

Configuration config = ConfigurationManager.OpenExeConfiguration("<app exe name>");

This where the exception was being thrown, not in the code in my question. Shame on me. Should have debugged that more thoroughly. I removed it all and replaced the local config object usage above with calls to:

ConfigurationManager.AppSettings["someValue"];

Upvotes: 2

Michael
Michael

Reputation: 557

It could definitely be a permission issue, and maybe more than just read permissions. Things to check:

  1. IS the task running under YOUR account, or another account? If this app is stored under a directory in your local profile then even other administrator accounts don't have permissions by default (they have to manually set permissions). Try moving the application to a local folder on the hard drive and set permissions as needed.

  2. Try setting audit settings for the directory your app is stored in. If it is a permission issue then the security log will tell you more about what and why it is getting denied.

Upvotes: 0

Related Questions