views:

415

answers:

5

I'm a C# novice running .NET 3.5, and I'd like to store a bunch of application default values in App.config, as the settings may vary by server environment (e.g. development, staging, production). What I want to do is similar to what's described in this StackOverflow article, but I also want to be able to use non-string values (e.g. int, bool). Something like this (name-values are just examples, btw):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <applicationSettings>
     <MyApp>
      <setting name="InitText" serializeAs="String">
       <value>Hello</value>
      </setting>
      <setting name="StartAt" serializeAs="Integer">
       <value>5</value>
      </setting>
      <setting name="IsWeekend" serializeAs="Boolean">
       <value>True</value>
      </setting>
     </MyApp>
    </applicationSettings>
</configuration>

Could somebody provide an example of how to do this, and how to retrieve the values via C#? I've seen a lot of examples that require using and , but I'm not sure if I need those elements, and if so, how to create them.

A: 

Sorry Application Configuration setting are always name value pairs and strings. You will have to create your own custom config section. Example

Anthony
Why not parse the strings?
Mike Atlas
Yes you could parse the strings but why? When you can create your own custom config section.
Anthony
+1  A: 

Boolean isWeekend = Convert.ToBoolean(ConfigurationManager.AppSettings["IsWeekend"])

JayG
Or Boolean.TryParse(...). http://msdn.microsoft.com/en-us/library/system.boolean.tryparse.aspx
Mike Atlas
doesn't this fail the "strongly-typed variables" portion of the requirements, you're still storing a string in app.config
Nathan Koop
Yeah, I don't want to have to recast a string as the proper type.
AspNyc
I was gong to add to my answer that this really isn't strongly typed for anything other than strings, but the phone rang. You would only need to use convert on non string data types. Still though, I upvoted João Angelo's answer, it looks like it might be a little slicker.
JayG
+7  A: 

What about using the Application Settings architecture of the .NET Framework. You can have strongly typed access to the default values.

On a Windows Application project the Settings file is created automatically in the Resources folder. You can then add application settings that are persisted in the App.config file just as you showed in your question.

For example:

int i = Settings.Default.IntSetting;

bool b = Settings.Default.BoolSetting;

Edit: If your project does not contain the settings file you can always add one by adding a New Item and then choosing a Settings File. (Right click project file and do: Add->New Item->Settings File). In my case I named it Settings but you can name it whatever you want.

After adding the file visual studio will open the settings designer in which you can add your strongly-typed settings. From what you said you should set the settings at the Application scope and not at the user. Then build the project and you should get access to a class with the name of the file.

João Angelo
+1 this stuff is baked into the .net framework
jvenema
I think this is what I need, but I'm unable to access the variables via C#. When I type "Settings." Intellisense doesn't see anything...
AspNyc
See the edit in my answer.
João Angelo
It's not really baked into the framework, it's rather an IDE thing. When you add a `.settings` file to your C#/VB project, it's used to generate code for class `Settings` that wraps config settings as properties of the appropriate type, and does all the necessary casts etc.
Pavel Minaev
Thank you for the clarification -- I appreciate it.
AspNyc
A: 

The thing JayG suggested can be done by the Visual Studio automatically. Use the appsettings wizard as described in the MSDN. Also the whole Application Settings infrastructure might be worth a read.

tobsen
A: 

Sounds like you want to have a custom . Basically, it works like this:

-Create a configuration class that has proper default values, etc, and set up a nice inheritance structure so it'll auto-load based on your web.config settings. -Add your config section to the web.config, specifying the "type" that will load your settings so the .NET framework will know what to initialize

You can find all kinds of info on the MSDN library about it. The examples are asp.net, but they work just fine with app configs as well.

jvenema