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

左心房为你撑大大i 提交于 2020-01-06 09:02:23

问题


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:
-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 startup.
- 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 = "ClientParameters/" + 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?


回答1:


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



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/4616962/vb-net-how-do-i-read-write-custom-config-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!