Link to home
Start Free TrialLog in
Avatar of CedarRapids
CedarRapidsFlag for United States of America

asked on

Changes to XXX.exe.config file are not retrieved on restart.

I have a Windows Service and I need it to reload its config file data when it is restarted so as to pick up any manual editing that may have occurred since the last build.

I reproduced the problem on a test Windows Form app.
Visual Studio 2015, .NET Framework 4.5.2, C#

“Name” and “Value” entries are made in VS 2015  Properties / Settings editing grid.
I can retrieve them using:
foreach (SettingsProperty currentProperty in Properties.Settings.Default.Properties)
{
    string name = currentProperty.Name.ToString(); // This shows all the "Name" fields in the config that existed after build time.
    string value = currentProperty.DefaultValue.ToString();  // This shows all the "Value" fields in the config that existed after build time.
}

But when changes are made to the X.exe.config file via text editor the above code will continue to display the same original values.
I can retrieve the new “Values” via the following code:

    string newValue = Properties.Settings.Default[currentProperty.Name].ToString();
… However, I don’t know how to get any new or modified “Name” fields with which to use to retrieve the values.
The store has the “Values” does it not also contain the “Name” keys?
Can I force a reread of the X.exe.config file at restart?
If it is caching, can I stop it?

Please help,
Thanks,
Jim – long-time member, seldom-user
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

Please try to load config in OnStart

protected override void OnStart(string[] args)
    {
        ...
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Snarf0001
Snarf0001
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CedarRapids

ASKER

Thanks for the prompt responses.
Re: Comment by Shaun
A load at OnStart does not force a full load to the point where the Keys and Value can be retrieved by
foreach (SettingsProperty currentProperty in Properties.Settings.Default.Properties)
The only way to retrieve new or changed “Names” is after rebuilding. A reboot does not even change them.
That is the whole point of my post.

Re: Comment by Snarf0001

What I have inherited is a Windows Service which stores its email Accounts and Passwords as the Name – Values. With type as Connection string), it makes it really easy to magically encrypt/decrypted. They wish to add accounts or change passwords with just a restart of the service. You say Properties.Settings isn’t intended for that. OK,
I believe you. It just seems odd I can access new dynamic “Values” but not a list of Keys.
A Windows Service does not have a Web.config. Are suggesting using the

So I made a hybrid X.exe.config with
<applicationSettings>
<connectionStrings>
<appSettings>
I can read the first set via Properties.Settings
I can then read the latter two sets via System.Configuration.ConfigurationManager.AppSettings & System.Configuration.ConfigurationManager.ConnectionStrings

Snarf0001? Are you really expecting 999 more Snarfs?

Thank for the help,
Jim