VB.Net 如何读/写自定义配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4616962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.Net How do I read/write custom config file
提问by Phil M
Environment: .Net 3.5 VB.net (C#ok too)
环境:.Net 3.5 VB.net(C#ok 也是)
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.
我编写了一个多项目 WinForms 应用程序,它需要根据用户对客户端的选择从配置文件的客户端部分加载几十个变量。还需要加载一些程序变量。到目前为止一切顺利,我将它们放在 app.config 文件中。
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>
...
在 appsettings 部分,我放置了主要的程序变量。例如<appSettings>
<add key="LocalServerName" value="PHILDESKTOP" />
......
我为可选择的部分创建了一个 ClientParameters 部分。例如
... <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
这是我的问题:
-我使用 Click Once 部署,并且很难找到 app.exe.config 文件进行更改
-我发现 app.config 文件不可写是有充分理由的..它在程序期间加载到内存中启动。
- 我发现我需要一个管理员才能在部署后添加/更新客户端部分参数。我想用 ListView 或其他东西来编程这种能力。
- 我想我写的代码很差,必须迭代才能找到设置,见下文
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.
所以我想要做的是将 app.config 的 client 部分分开并制作类似 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
我需要一些很好的示例 XML 读/写代码:
- 加载 client.config 文件
- 选择特定的客户端部分
- 从该部分中的值加载我的客户端变量
Anyone got some good links or advice?
有人有一些好的链接或建议吗?
回答by Chris Haas
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.
你可以只使用内置设置吗?这是一个教程。任何范围为用户的设置都可以使用My.Settings
“命名空间”进行编辑。没有读/写 XML,一切都是强类型的。任何范围为 Application 的东西基本上都是只读值。
'Set value
My.Settings.FirstName = "Chris"
'Load value
Dim F = My.Settings.FirstName
'Persist values
My.Settings.Save()
回答by Hans Passant
This is an XY question. You are asking for a solution for Y while the realproblem is X. AppSettings are supposed to be easyto read. When you find yourself in a situation where it is suddenly no longer easy to read then an AppSetting is useless to you.
这是一个XY问题。您正在为 Y 寻求解决方案,而真正的问题是 X。AppSettings 应该易于阅读。当您发现自己突然不再容易阅读时,AppSetting 对您毫无用处。
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.
不太确定什么是更好的解决方案,您的问题中没有很好的提示。在我看来,ClickOnce 是让您遇到麻烦的原因。W问题。
回答by BenAlabaster
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.
我写了一篇关于阅读自定义配置部分的博客文章,它允许您编写一个全面的自定义 app.config 部分并从您的代码访问它。这个过程不是我所说的直观,但我详细介绍了它。一旦您为读取自定义部分设置了类模型,就可以轻松引用属性。