VB.Net如何读取/写入自定义配置文件 [英] VB.Net How do I read/write custom config file

查看:85
本文介绍了VB.Net如何读取/写入自定义配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:.Net 3.5 VB.net(也是C#ok)

我编写了一个多项目WinForms应用,根据用户对客户端的选择,从配置文件的客户端部分加载数十个变量。同样,一些程序变量也需要加载。到目前为止一切顺利,我将它们放在一个app.config文件中。

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.

在appsettings部分中,我放置了主程序变量。例如,

< appSettings>

< add key = LocalServerName value = PHILDESKTOP />
...

,我为可选择的对象创建了ClientParameters部分。例如。
< ClientParameters>

< Cust1>

<设置名称= ClientName serializeAs = String>

< value>客户名称1< / value>

< / setting>


...

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>

...

这是我的问题:

-我是使用单击一次部署的,很难找到app.exe.config文件进行更改

-我发现有一个很好的原因,该app.config文件不可写..它在程序启动时被加载到内存中。

-我发现我需要管理员才能部署后添加/更新客户端部分参数。我想用ListView或其他工具来编程该功能。

-我想我写的代码很烂,必须迭代才能找到设置,请参见下文

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

所以我要做的是拆分app.config的client部分,并制作诸如client.config之类的东西。

So what I want to do is to split off the client section of the app.config and make something like client.config.

我需要一些良好的考试完整的XML读/写代码为:

-加载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?

推荐答案

您可以只使用内置设置吗? 这里是一个教程。可以使用 My.Settings 命名空间来编辑范围为用户的任何设置。无需读写XML,所有内容都经过严格键入。范围为Application的所有内容基本上都是只读值。

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

这篇关于VB.Net如何读取/写入自定义配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆