1

I created a settings file.

There is a field UseXmlPatternForTestServer which i a bool. I set the scope to appplication and value to True. I can see it added some content to the .config file.

After compilation a modified the .config file in build directory:

<configuration>
    <applicationSettings>
        ...
        <Logic.Properties.Settings>
            <setting name="UseXmlPatternForTestServer" serializeAs="String">
                <value>False</value> // **modified to false**
            </setting>
        </Logic.Properties.Settings>
    </applicationSettings>
</configuration>

Even though the value is set to False, the line below returns True. Why? And how can I get the current value from the config file?

Properties.Settings.Default.UseXmlPatternForTestServer // returns true

edit

All the settings above are in a class library project referenced by my app. Maybe that's the problem?

Andrzej Gis
  • 13,706
  • 14
  • 86
  • 130
  • 1
    Right click your project in solution explorer and click properties a new tab should open in the middle and then on the tabs to the left click on settings. See what value is shown in there and make sure its updated to what you want it to be. it might even pop up and say a setting was modified would you like to update it or something along those lines to. – Bearcat9425 Jul 12 '13 at 14:02
  • It's set to True as it should be. The thing is I want to change it manually to false after build. – Andrzej Gis Jul 12 '13 at 14:03
  • I believe that setting the scope to application makes the values readonly. – Logarr Jul 12 '13 at 14:57
  • If you build and you check the file did it get changed back to true? It could be just that your changes are getting overwritten when you rebuild the project. – Scott Chamberlain Jul 12 '13 at 16:27
  • From your last edit (*All the settings above are in a class library project referenced by my app. Maybe that's the problem?*) i would recommand you to take a look at this http://stackoverflow.com/questions/17272956/location-of-app-config-file-used-by-referenced-library-for-my-settings/17286006#17286006 – Chris Jul 12 '13 at 17:28

2 Answers2

1

Try using settings directly without class library project or change the scope of UseXmlPatternForTestServer to User instead of Application.

See User Settings in C#

Software Engineer
  • 3,906
  • 1
  • 26
  • 35
1

I encountered the same problem and because the scope 'user' didn't work for me either, I ended up creating my own XML config which I can serialize / deserialize and manage how I want it to be. That's also nice because you can store whatever you want in your config file (for example a list of objects).


Basically your model could look something like this:

public class Config
{
     public string UseXmlPatternForTestServer {get;set;}
     //your properties to store

}

Serializer class to load / save your config:

public static class XmlConfigSerializer
    {

        public static Config DeSerialize()
        {
                try 
                {           
                    if (!File.Exists("config.xml")) { return null; }

                    XmlSerializer serializer = new XmlSerializer(typeof(Config));
                    using (var fs = new FileStream("config.xml", FileMode.Open))
                    {
                        return (Config) serializer.Deserialize(fs);
                    }                   
                }
                catch (Exception ex)
                {
                    //log error
                    return null;
                }
        }

        public static void Serialize(Config config)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Config));
            using (var fs = new FileStream("config.xml", FileMode.Create))
            {
                serializer.Serialize(fs, config);
            }
        }
    }
Fabian Bigler
  • 10,403
  • 6
  • 47
  • 70