Using App.Config for user defined runtime parameters

These are the steps involved in defining runtime parameters and using them in your code.

1. Create a App.Config file
2. Define the runtime parameters and provide values for these
3. Access these parameters in your code
4. Verify changing the parameter value at runtime affects the application


Step 1: Create App.Config file

1a. Create a Console Application
1b. In Solution Explorer, right click on project. Add->New Item. Select Application Configuration file. This file will be added to your project.


Step 2: Define the runtime parameters and provide values for these

2a. Open App.Config.
2b. Define the keys and values for your runtime parameters.

Example:

<configuration>
<appSettings>
<add key="OperatorName" value="Rita"></add>
<add key="LoggerLevel" value="5"></add>
</appSettings>
</configuration>

You can define any key and provide values. Ensure you don’t have duplicate keys.


Step 3: Access these parameters in your code

3a. Add a refernce System.Configuration DLL.1
3b. Add the following lines in your code. You can directly add these lines in Main.

string name = System.Configuration.ConfigurationManager.AppSettings["OperatorName"];
Console.Writeline("Welcome " + name);
string level = System.Configuration.ConfigurationManager.AppSettings["LoggerLevel"];

Console.Writeline("Logger level: " + level);

3c. Build your project and execute it.The output should be:

Welcome Rita
Logger level: 5


Step 4: Verify changing the parameter value at runtime affects the application

4a. When you build your project, the App.Config file is copied to your Debug/Release folder with the name .exe.config. My project is named UseAppConfig. The App.Config in my Debug folder is UseAppConfig.exe.config

Open the .config file in your working directory (Debug/Release). Change the values to Mary and 1. In the same folder you will see your executbale. Double click on this and you should see the new values.2

You are all set. You can identify all those parameters that make sense as runtime parameters, define a meaningful key for each parameter and start using it. You no longer have to build your application every time you want to change the parameter value.


Notes

1: You need this reference only if you are using .NET 2.0
2: If you change the App.Config from Visual Studio IDE, then you need to rebuild your project to see the new values.

About these ads
Tagged

36 thoughts on “Using App.Config for user defined runtime parameters

  1. swetha says:

    This info was very useful . The minute info for adding the reference for .net 2.0 really saved me. :)

    • mkayani says:

      Thanx Anamica. the article really helped. I would say that it was a gud approach to help out the beginners in .NET and using VS. I can see many comments below and above so i would appreciate your effort.
      Atleast it helped me. So thanks again..

  2. Pat says:

    This is passing the parameters to the application before running the application and not at the run time(You haven’t demonstrated how to change the values while the application is running. Sorry to pick on this)

  3. Anaamica says:

    I don’t understand you, Pat. If you mean persisting values back in App.Config when the application is running, then that is not advised. App.Config is meant to be used for passing values to the application and not the other direction.

  4. Joe Shmoe says:

    The xml is case sensitive and it should say “appSettings”, not “appsettings.” Shame, shame!

  5. Anaamica says:

    Yeah, shame on me! Where would I be without readers like you?

  6. DMMcGrue says:

    This information isvery useful. However, I’m needing to use an app.config to pass SQL connection strings on a compact framework 2.0 hand held device as well as a desktop. Can you give an example on how this can be accomplished? I’m new to the .NET environment.

    Thanks,

    DMMcGrue

  7. Prajeesh says:

    I want to send an array of strings using my app.config file. Is it possible ?

  8. Anaamica says:

    If you see the config file, all you can pass is a string. If you want to pass an array of strings, then you have to come up with a protocol.

    You can pass each string with a delimiter of your choice: “string1;string2;string3;string4″ and when you read this value in your code, you will get one string value which you have to split based on the delimiter. So you will end up with a string of arrays with individual values.

  9. Graham Epps says:

    Thank you. Very good example.

  10. Elen Alonso says:

    What if you want to change the values during runtime; I mean in the config file. Then the changed values won’t get recognized by the application. For example write a simple application which reads value from the app.config and displays it in a message box. Now after launching the application (without exiting the application), if you try to change the value in the app.config, it won’t show the new value in the message box. Do you have any solution for this (something like a ASP.NET web.config file where you can change the values even when the web application is running)

    Thanks

  11. Anaamica says:

    App.config file should not be updated during run time. It is not meant for that. It is only meant for reading values and not writing to it.

  12. Elen Alonso says:

    Well………. Is there a alternate solution there? Coz I’ve written a windows service. The config file hold some values which will be used by the service. What if I want to change values without restarting the service. I need an alternative solution.

    Thanks

  13. Jim says:

    You example is great, it works fine with 2005. However I built an application with .net 2003. It doesn’t have config manager can you help???

    Thank you Jim

  14. Magnus says:

    You write: “App.config file should not be updated during run time.” in an answer to someone asking how they can change the value during runtime making the application use the new values.

    The reason is because of “Step 4: Verify changing the parameter value at runtime affects the application”

    It should be called “Step 4: Verify that changing the parameter value affects the application” since changing the values during runtime does nothing until you restart the application.

    Besides that, great example! Thanks!

    Magnus

  15. sumanth says:

    hi how to update the app.config values at run time

  16. AllanN says:

    // Get the configuration file.
    System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    //Get the appSettings section.
    AppSettingsSection appSettings = (AppSettingsSection)config.GetSection(“appSettings”);

    appSettings.Settings.Add(“test”,”value”);

    config.Save();

    this will update the appname.exe.config file.

    Be careful though, this only updates the copy in your bin folder. if you update your copy (probably app.config) in your solution, and redeply, you’ll probably overwrite any saved values.

    You really should be using a database or similar for user-specified configuration data.

  17. rudi says:

    Hi,

    I want to configure a program I wrote during installation. So I added an Installer class to my program that takes some parameters from the user during setup and writes to the app.config. The writing to the app.config part doesn’t work.
    Is is posible to write to the App.config file during the itstallation of the program (during the execution of the setup – .msi file)?

    I succeeded in writing to the app.config from my program (not during setup) but after the config.save() the new values are saved but all the comments I had in my app.config were removed and the order of the keys was changed… Is there a way to save the new keys without the removal of the comments and reordering of keys?

    Thanks!

    • Kajod says:

      Rudi, I’m also facing the same problem. I’m changing the app.config at runtime, but comments written in it are removed with saving config file. Have you got any solution? If yes, it would be very helpful if you share the solution. Thanks in advance.

      Regards,
      Kajod

  18. Alon says:

    How do u do it in VB.Net ?

  19. [...] if you are a .Net developer, you know about the venerable app.config, which is used to set program parameters at run-time, rather than at design time. Because these can [...]

  20. Gladiator says:

    Anaamica,

    This is a very basic code is .net. And probably for beginners and top of it the subject of the article is misleading – Using App.Config for user defined runtime parameters. Don’t call it runtime parameter. Runtime parameters can dynamic. I agree with Pat. By the way is Swetha your friend or eerr sister? Only she seems to have appreciated your article.

    G

    • LOL @ Gladiator says:

      Gladiator,
      Are you Pat’s brother/dad/dog/partner/slave? Only you seem to have agreed with Pat.

      Most of us do agree that this a good article (Me, Adorjan, Jim, Graham, DMMMcGrue, etc.)

  21. Adorjan says:

    Great article, Anaamica!
    Keep up the good work! ;)

    And for those who don’t know what runtime means, it means the time when the application is compiled and ready to use. Runtime DOES NOT means the time when the application is already running (opened in the process tab). Yeah, I know that it’s confusing, but english language don’t have a separate word for every little thing. :) So, runtime in coding language means the time when the application is already compiled. Runtime in the sense of an application means the time when an application is running.
    So, if somebody tells you that he had some errors during runtime of your application, that means the time while your application was executing on the target machine. But if a coder tells you to change some parameters on runtime, that means to change some parameters after your application compiled, which is the time before you actually start it.

    I hope I helped somebody non-english to understand a difference about runtime application. :)

  22. Greg says:

    Thanks this really helped me out, esp the part about the reference to system config… my company still insists on using .NET 2.0 and it saved me a hassle.

  23. LOL @ Gladiator says:

    Gladiator,
    Are you Pat’s brother/dad/dog/partner/slave? Only you seem to have agreed with Pat.

    Most of us do agree that this a good article (Me, Adorjan, Jim, Graham, DMMMcGrue, etc.)

  24. An-droid says:

    Thank you

  25. chenn says:

    Nice clean description to begin, many thanks to Anaamica ….

  26. Tobberoth says:

    Good article, but Pat and Gladiator are correct, it’s not a changing runtime parameters if you aren’t changing them during runtime, which is when the program is actively running. The fourth list item clearly says “Verify changing the parameter value at runtime affects the application”, which can’t happen since if you change the parameter during runtime, it doesn’t affect the application, it affects the application if you change it before running the application, which is not at runtime.

  27. Ramesh says:

    Awesome article. Previous programmer had deleted the app.config file for reasons that are not evident to me. Your article helped me bring the client back up and running in no time

  28. Derek says:

    Hmm is anyone else encountering problems with the images on this blog loading?
    I’m trying to find out if its a problem on my end or if it’s the blog.
    Any responses would be greatly appreciated.

  29. Tomek says:

    thank you.

  30. Woah this kind of website will be wonderful i adore reading your content. Continue to be inside the good artwork! You’re sure, plenty of folks want all-around with this details, you are able to help them significantly.

  31. Neeraja says:

    Awesome website, a beginner can get enough knowledge

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: