Phil M
Phil M

Reputation: 11

VB.Net How do I read/write custom config file

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> 
    <add key="LocalServerName" value="PHILDESKTOP" />

and I created a ClientParameters section for the selectable ones. E.g.

<ClientParameters>  
    <Cust1>  
        <setting name="ClientName"   serializeAs="String">  
            <value>Customer Name 1</value>  
        </setting>

Here's my problem:

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:

Anyone got some good links or advice?

Upvotes: 1

Views: 44171

Answers (4)

dceuinton
dceuinton

Reputation: 31

Noticed that the link was broken in @Chris Haas' answer. But I didn't have enough reputation to comment on his post (rip) so I'll add this answer to help anyone who might need this later.

Here is the Microsoft tutorial - Accessing application settings (Visual Basic). This should provide a better overview on application settings and how to access and use them.

Upvotes: 0

BenAlabaster
BenAlabaster

Reputation: 39846

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

Upvotes: -1

Hans Passant
Hans Passant

Reputation: 941665

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.

Upvotes: 2

Chris Haas
Chris Haas

Reputation: 55437

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()

Upvotes: 5

Related Questions