vb.net 读/写“INI”文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16755029/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:44:47  来源:igfitidea点击:

Read/Write "INI" file

vb.netini

提问by George Vaisey

Evening Everyone -

晚上大家——

I'm looking for some thoughts on how to read / write values from a windows "ini" structured file. I have a settings file created with another application and I would like to update values of a key within a specified section. I got it working using a buffer.replace process but now realize that some keys are used over in sections and globally replacing a value will cause problems.

我正在寻找有关如何从 Windows“ini”结构化文件读取/写入值的一些想法。我有一个用另一个应用程序创建的设置文件,我想更新指定部分中某个键的值。我使用 buffer.replace 过程让它工作,但现在意识到某些键在部分中被使用并且全局替换值会导致问题。

Here is a sample of what my ini file looks like

这是我的 ini 文件的示例

IMPORT-1]
SETTINGS="HELLO"
FILENAME="C:\TEST\TEST1.CSV"

[ENCODE-2]
FILENAME="C:\TEST\REPORT1.XPS"

I've got dozens of blocks so any clarity on accomplishing a read and write of a value within a specific section would be hugely appreciated!

我有几十个块,因此在特定部分内完成读取和写入值的任何清晰度都将不胜感激!

--Cheers & Thanks George

——干杯和感谢乔治

回答by the_lotus

You can use some of the kernel32 functions.

您可以使用一些 kernel32 函数。

Private Declare Auto Function GetPrivateProfileString Lib "kernel32" (ByVal lpAppName As String, _
        ByVal lpKeyName As String, _
        ByVal lpDefault As String, _
        ByVal lpReturnedString As StringBuilder, _
        ByVal nSize As Integer, _
        ByVal lpFileName As String) As Integer

This will let you read an ini file

这将让您读取 ini 文件

Dim sb As StringBuilder

sb = New StringBuilder(500)
GetPrivateProfileString("IMPORT-1", "SETTINGS", "", sb, sb.Capacity, "test.ini")