Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have been trying to follow the MSDN instructions for adding settings to my WPF desktop application. I have added a setting using the designer and this results in a file called MyApp.exe.config in the \bin project output directory. Opening this file, I find my setting. Everything good so far.

Then I go to this page Accessing settings at run-time, which explains how to read a setting at run-time. The problem is that I have a Properties instance in my application constructor but it has no Settings member?

What am I doing wrong?

Kind wishes ~ Patrick

What I have tried:

I have tried following the MSDN documentation.
Posted
Updated 15-Jul-16 3:22am
v2
Comments
G3Coder 15-Jul-16 5:39am    
Hi Patrick,

Can't find anything in the MSDN explaining it. To me it looks like the data isn't loaded yet.

Even an attempt with System.Configuration.ConfigurationManager.AppSettings doesn't yield any proper results.

Depending on the whys and hows I suggest an old school file storing settings.

Good luck.
G
Patrick Skelton 15-Jul-16 5:41am    
Thanks for the reply. Do you have any idea why the Properties.Settings member is not even present? Do you think the docs are wrong?
G3Coder 15-Jul-16 6:10am    
I don't think it has been initialized yet so the .Settings isn't there yet. The documents don't say you can do this exactly - more they show examples once everything is up and running, so I would struggle to say they are wrong.
The Properties.Settings method does work in the constructor of the MainWindow() as per the documentation.
Patrick Skelton 15-Jul-16 7:26am    
You are exactly right, sir. The MSDN example works fine when moved to the MainWindow. Seems like a pretty major flaw to me. My requirement to access a setting prior to displaying the first window doesn't seem unreasonable. What I am wanting to do in fact is change the StartupUri depending on a setting, so I will have to work around this. Thank you for your help.
G3Coder 15-Jul-16 7:55am    
Pleasure :) Enjoy the weekend.

1 solution

The issue is that the System.Windows.Application class[^] has a property called Properties[^], which is hiding your application's Properties namespace. Any code within the Application class which tries to access Properties is looking at the property, not the namespace.

To work around the problem, you can either use the fully-qualified name to refer to the settings:
C#
var aSetting = YourNamespace.Properties.Settings.Default.SomeSetting;

or you can add an alias for the Settings class:
C#
namespace YourNamespace
{
    using Settings = Properties.Settings;
    
    public partial class YourApplication : Application
    {
        public YourApplication()
        {
            var aSetting = Settings.Default.SomeSetting;
        }
    }
}
 
Share this answer
 
Comments
Patrick Skelton 15-Jul-16 9:35am    
Perfect! Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900