Reading and writing a 'Config File'

vis781

Well-known member
Joined
Aug 30, 2005
Messages
2,016
Location
Cambridge, UK
Programming Experience
5-10
Reading and writing a 'Config File' [Resolved]

Hi there, anyone out there who can explain something to me please.....?

I would like to store my database connection string in a application config file however there are two problems i've encountered...

Firstly the only config file that i seem to be able to make is a web config file does this matter or is there a way of changing it to an application config file. To do this i'm simply going to add new item 'config file' and it always puts a web config file in by default.

Secondly i've heard that you can't or shouldn't write to a config file at runtime, i dont know why i just read somewhere that it's not a good idea. Is this true? If not is there a specific way to do this. And if it is true anyone got any good ideas on where to store my connection string??

Any help greatly appreciated.
 
Last edited:
i would recamend saving your applications configuration settings (including the connection string) in an xml file

the DataGrid control has a ReadXml and WriteXml methods for the actual reading and writing of the file, just use the datagrid as the reader/writer
 
The DataGrid cannot read and write XML, but the DataSet can. As for your connection string, The ConnectionString property of a connection object is a dynamic property, so all you have to do is edit the DynamicProperties section in the Properties window for a connection that you have added to a form in the designer and you're done. The config file will be created automatically if one does not exist. The entry will be added to the config file automatically. The connection string will be loaded automatically at startup. If you want to be able to change the ConnectionString at run time then check out the following link:

http://msdn.microsoft.com/library/d...spreservepropertysettingsinvisualbasicnet.asp

Just be aware that the config file is read once and once only. Any changes made will not be reflected through the System.Configuration namespace until the app is restarted.
 
Back
Top