Title: VB.Net How do I read/write custom config file
Tags: vb.net;configuration-files
Question: Environment: .Net 3.5 VB.net (C#ok too)
I wrote a multi-project WinForms app that needs to load a couple dozen variables from a client section of a config file based on user's selection of client. Also some program variables need to be loaded as well. So far so good, I put them in an app.config file.
In the appSettings section I put the main program variables. e.g.
<appSettings&gt;
<add key=&quot;LocalServerName&quot; value=&quot;PHILDESKTOP&quot; /&gt;
and I created a ClientParameters section for the selectable ones. E.g.
<ClientParameters&gt;
<Cust1&gt;
<setting name=&quot;ClientName&quot;   serializeAs=&quot;String&quot;&gt;
<value&gt;Customer Name 1</value&gt;
</setting&gt;
Here's my problem:
I deployed using Click Once and the app.exe.config file is hard to find to make changes
I discovered that the app.config file is not writeable for a good reason.. it is loaded into memory during program start-up.
I find I need an admin to be able to add/update the client section parameters after deployment. I want to program that ability with a ListView or something.
I think I have written poor code that must iterate to find a setting see below
Dim sectionName As String
sectionName = &quot;ClientParameters/&quot; + ClientCode
Dim section As System.Configuration.ClientSettingsSection = _
DirectCast(System.Configuration.ConfigurationManager.GetSection(sectionName),  _
System.Configuration.ClientSettingsSection)
For Each setting As System.Configuration.SettingElement In section.Settings
Dim value As String = setting.Value.ValueXml.InnerText
Dim name As String = setting.Name
If name.ToLower = SettingName.ToLower Then
Return value
End If
Next
So what I want to do is to split off the client section of the app.config and make something like client.config.
I need some good example XML read/write code to:
load the client.config file
select a particular client section
load my client variables from the values in that section
Anyone got some good links or advice?
Comment: Did you find a solution for this? Seems obvious one would want a read/write config file with an app if there is no DB attached.


Here is another answer: I wrote a blog post about reading custom configuration sections that allows you to pretty much write a comprehensive custom app.config section and access it from your code.  The process is not what I might call intuitive but I covered it in a fair amount of detail.  Once you've got the class model set up for reading the custom section, it's a cinch to reference the properties though.
http://www.endswithsaurus.com/search/label/app.config
Comment for this answer: Link broken, please do not post links.
Comment for this answer: Apologies, this has been fixed

Here is another answer: This is an XY question.  You are asking for a solution for Y while the real problem is X.  AppSettings are supposed to be easy to read.  When you find yourself in a situation where it is suddenly no longer easy to read then an AppSetting is useless to you.
Not so sure what a better solution might be, no great hints in your question.  Sounds to me that ClickOnce is what's getting you in trouble.  The W problem.
Comment for this answer: Fair enough.. good thoughts. I think it is ClickOnce that makes it hard.  Still, I want to have a separate custom.config file that I can read/write. I used a .ini file before, it was simpler.

Here is another answer: Can you just use the built-in settings? Here's a tutorial. Any settings scoped as User can be edited using the My.Settings "namespace". No reading/writing XML and everything's strongly typed. Anything scoped as Application are basically read-only values.
'Set value
My.Settings.FirstName = "Chris"
'Load value
Dim F = My.Settings.FirstName
'Persist values
My.Settings.Save()
Comment for this answer: Yeah, I might reconsider that. User variables are indeed read/write. Might be the simplest way to manage. Thinking it over.. maybe my mindset is stuck in the old .ini paradigm.
Comment for this answer: well thanks @Chris Haas, that is goo tutorial for User scope. BUt how about if it's Application scope? How do we change its value without we re-run the app? my dataset (database) is stored in Application scope using its connection string... sigh. :(

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2011:00:01/06/11', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<vb.net><configuration-files>', 'Title': 'VB.Net How do I read/write custom config file', 'ViewCount': 41462, 'id': 4616962, 'user_AboutMe': None, 'user_AccountId': 693653.0, 'user_DisplayName': 'Phil M', 'user_Reputation': 11.0, 'user_Views': 11.0, 'user_id': 565707.0}}