0

This is my first time working with config files, so I am not sure which direction to go. I have looked around but have not found any specific answers.

I have a some configuration settings, namely a list of integers, a list of custom objects and a single integer. At the moment, I am pursuing XML for my config file, I am using serialisation to store and read my list of custom objects. However, I am not sure of the convention or the possiblitly of storing the other settings in the same file. Ideally I would like it to be a single file configurable via a GUI, can I acheive this with XML?

How can I store and read my other list, and single int independently? i.e. refer to that part of the file? - does serialisation allow for this or do I need multiple files? Would that be bad practice?

Should I use a differnt type of config file, or use settings in addition to my XML file? Is it bad practice to have more than one mode of configuration?

Many thanks for any advice you can provide!

marked
  • 589
  • 9
  • 24

4 Answers4

1

If its a windows service you probably want to use the registry

If its a WCF service then there is a well defined XML format for them that uses the .net .config format.

You can add custom settings in here and also custom sections with their own format rules.

the_drow
  • 18,571
  • 25
  • 126
  • 193
John Nicholas
  • 4,778
  • 4
  • 31
  • 50
  • We make heavy use of app.config (the .NET config files) for configuration of standalone applications and .NET-based Windows Services. The custom sections are really quite nice for grouping the configuration settings. – jglouie Jun 29 '11 at 15:00
  • Sorry, should have specified. Its a windows service that I would also like to port with mono to be cross-plafform. If I were to use .net config files, could I store a list of custom objects? I am a bit nervous about using the registy, I am not sure what it involves. – marked Jun 29 '11 at 15:08
  • Yes, you can store .net objects in config files. Really you are talking about serialization ... but if you can get away with config files, that will probably make your life simple. serialisation is really not hard though ... – John Nicholas Jul 04 '11 at 12:46
1

I used to store configuration information in app.config file (not separate). However working for big companies I found that on production you don't have access to app.config file (only IT personal can have access to production). So if you need to change configuration you need to go through change request, and it takes time. So in this case I started to store my configuration information in database. This way I can change at any time and I don't need go through change request.

About separate xml or app.config. I would use separate config file in case if you have a lot of config information, or information that is structured/nested, but most of the time (99%) I would use just app.config for this.

Check this out how to store int array in application settings

Community
  • 1
  • 1
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
  • The thing is I have two lists, but I am not sure if I can store them in the same config(XML) file and serialise. When you say seperate file, do you mean more than one? I would like to keep all the settings in one file, but is it forgivable not to? – marked Jun 29 '11 at 15:18
  • You can store any number of lists of serialisable objects in config file, and yes you can keep them all in the same file. One of the options you can try is to go to the project properties Click on Settings tab on the right and then click on provided link in the middle. It will gives you list of objects that you can serialize in your config file. Here is an example http://msdn.microsoft.com/en-us/library/a65txexh.aspx You should be able to serialise list if you select it from Browse option. Check this out http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx#settingscs_topic2 – Vlad Bezden Jun 29 '11 at 15:50
  • Do you think it may be possible to adapt the following technique... http://stackoverflow.com/questions/1321248/how-do-i-save-serialize-a-custom-class-to-the-settings-file to use a list of custom object like I require? – marked Jun 29 '11 at 17:05
0

Check this out How to store int[] array in application Settings

Community
  • 1
  • 1
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
  • This is interesting, I will investigate a bit further and see if a split string suits what I need. – marked Jun 29 '11 at 17:12
0

What version of .NET / VS.NET do you have ? VS 2008 and later, you can add a custom configuraction file, that is a XML internally, but can be modified by a custom grid editor in the IDE

umlcat
  • 4,091
  • 3
  • 19
  • 29
  • Im using Visual C# 2010 Express. What do you mean by 'custom' configuration file? Im using an xml file at the moment, but would like retrieve more than one set of data from it, i.e. two seperate lists and a sinlge value. Its the selecting what I need from the file that im unsure about. Could this help, and if so, could you elaborate? Im a bit of a novice :P – marked Jun 29 '11 at 16:59
  • @market I don't know if "Express" includ this. In the "Solution Explorer", when the mouse right button is clicked and a contextual menu appears, there is an option "Add New Item", that allows to add not just a form, XML u other files, but a "Settings file", that is XML based and has an integrated custom editor – umlcat Jun 29 '11 at 18:33