I was trying to learn how to add and use User settings and Application settings.

And somehow I've managed to create some kind of double setting.

Here is my app.config file.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a6c561934e089" >
            <section name="TestApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a6c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <TestApp.Properties.Settings>
            <setting name="CsvPath" serializeAs="String">
                <value>value</value>
            </setting>
            <setting name="Sweet" serializeAs="String">
                <value>sugar</value>
            </setting>
        </TestApp.Properties.Settings>
    </userSettings>
</configuration>

In my code I have this.

Properties.Settings settings = new Properties.Settings();
            string s = settings.CsvPath;
            if (!String.IsNullOrEmpty(s))
            {
                MessageBox.Show(settings.CsvPath);
            }
            else
            {
                MessageBox.Show("Not Set");
            }

I would expect my message box to show "value" but it shows "OK" which is a setting I tried earlier, which I thought was gone.
Forgotten how to post an image.

http://s8.postimg.org/7mbu6nrsl/settings.jpg

Almost forgot my question.
What on earth is going on here?

(edit)
It's quite possible that earlier I had application setting as CsvPath, but it's not showing in xml.

Any chance of getting the entire app - zipped maybe?

I'm really not there yet.

If I add a line of code to set the CsvPath variable like this.

Properties.Settings settings = new Properties.Settings();
            settings.CsvPath = "value";
            string s = settings.CsvPath;
            if (!String.IsNullOrEmpty(s))
            {
                MessageBox.Show(settings.CsvPath);
            }
            else
            {
                MessageBox.Show("Not Present");
            }

Then my message box shows "value".

Then if I comment that line out.

Properties.Settings settings = new Properties.Settings();
            //settings.CsvPath = "value";
            string s = settings.CsvPath;
            if (!String.IsNullOrEmpty(s))
            {
                MessageBox.Show(settings.CsvPath);
            }
            else
            {
                MessageBox.Show("Not Present");
            }

it is back to showing "OK".

The app.config you showed is that the one in your solution, or the one in your debug output folder? If that file is not overwritten when built, the running code may still have the old value. Either manually check, or do a "clean solution" to remove all old files.

I have solved the problem.
In my windows user folder.

"C:\Documents and Settings\Susan\AppData\Local\TestApp\TestApp.vshost.exe_Url_cwevwrj1hnmqmdoc5cs2zsf2tn5j1m1g"

There was a file user.config.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <TestApp.Properties.Settings>
            <setting name="CsvPath" serializeAs="String">
                <value>OK</value>
            </setting>
        </TestApp.Properties.Settings>
    </userSettings>
</configuration>

I deleted it, and now my code is acting as expected.

Still unsure how I did it though and therefore how to prevent it.

How do I distinguish between reading app.config and user.config, I get the feeling deleting that file is going to cause problems ahead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.