Read/Write “INI” file

谁说我不能喝 提交于 2020-01-14 10:39:08

问题


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.

Here is a sample of what my ini file looks like

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


回答1:


You can use some of the kernel32 functions.

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

Dim sb As StringBuilder

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


来源:https://stackoverflow.com/questions/16755029/read-write-ini-file

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