views:

101

answers:

3

For our approach we want to create one of the *.config files from scratch and populate it with some default/custom values at runtime.

Is there possibility to do this programatically via ConfigurationManager or something like that?

+2  A: 

Since .config files are just XML, you should be able to just use an XmlTextWriter to build your config file.

Tim S. Van Haren
Yeah... this makes sense. Will see if people have more smooth approaches and if not will accept this answer. Thanks!
Andriy Buday
@Andriy - This answer should be used to create the initial file for sure, then use the `ConfigurationManager` class thereafter - however, why not just use it as it is defined right now? Is there any purpose?
Kyle Rozendo
Actually, other developers within my project asked me about this, so I'm not sure why they need such cumbersome approach. Maybe, I have to find this out.
Andriy Buday
A: 

sure it is, it's a simple and plain xml file

now... what config file are you writing about? app.config? web.config? custom.config?

first two are configuration support for the windows app and web app, so you would need to generate from an "outside" app and then fire that app up (witch that already happens with the Setup project where it asks for the user things and you can edit/add to the configuration file, like Database connections, etc)

balexandre
It should be app.config. I don't want to use any "outside" app.
Andriy Buday
you can have a Setup window and write to it, it is not protected, then ask to restart (or restart the app automatically) so it can reload all new stuff. Though I would use the Setup if it's only one time change, has you did not mention what do you want to add/change. :)
balexandre
+3  A: 

Yes. As you point out, the ConfigurationManager class allows you to read and write config files.

ConfigurationManager Class

Scroll down a bit.

Sure, you can read / write these files as XML files, but the above class exposes a much handier interface for manipulating config files.

Ziffusion
I didn't know this. An example of why I browse stackoverflow on a daily basis.
Bryce Fischer
Actually, as I see it doesn't allow me save file initially. It allows me open existing config and make changes to it, then save. So looks like idea with creating file with File/XML Writer is good.
Andriy Buday
Ok, ConfigurationManager is indeed good for manipulating existing config file, but we will have to create it with XMLTextWriter, as is mentioned in other answer.
Andriy Buday