问题
I have a windows app that uses SettingsProvider to read configuration settings and sets default values if file does not exist.
It works fine running normally.
I am trying to write a windows service that starts this app. When it is run by the service, I get System.Configuration.SettingsPropertyNotFoundException
on all the setting attributes.
How can I resolve this exception when the service is running the app?
回答1:
This simply means that the app can't read the .Settings file. I can think of two possible causes:
The service runs under an account that doesn't have access to the .settings file. (or .config file, depending) This is unlikely because the service can start the app, and it wouldn't make sense for it to have permissions to the app and not the settings file.
The runtime can't find the settings file. It expects the settings to be in the root startup path of the executable. Check to ensure that it exists on the machine in question.
However, a google result turned up an obvious possible cause I haven't thought of. Were the .settings added after the last compile? Compile the app in Visual Studio, and try again...
回答2:
Another possible cause is if you write a custom SettingsProvider
that is throwing and exception during Initialize
.
In my case, I had done this:
public class CustomSettingsProvider : SettingsProvider
{
public override void Initialize(string name, NameValueCollection config)
{
base.Initialize(name, config);
}
}
Since name
is always passed as null
, base.Initialize
was throwing an ArgumentNullException
. I fixed it by passing a non-null name like this:
public override void Initialize(string name, NameValueCollection config)
{
base.Initialize(name ?? GetType().Name, config);
}
来源:https://stackoverflow.com/questions/8404832/i-get-a-system-configuration-settingspropertynotfoundexception-error-in-windows