vba 读取整个 ini 部分并放入数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5456997/
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
Read entire ini section and put into array
提问by Kenny Bones
Ok, so I have these functions I'm tring to use via my vba code. It's probably the as it would have been with vbs as well.
好的,所以我有这些功能,我想通过我的 vba 代码使用。它可能也和 vbs 一样。
Here's the function(s)
这是函数
'declarations for working with Ini files
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias _
"GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, _
ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias _
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, _
ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, _
ByVal lpFileName As String) As Long
'// INI CONTROLLING PROCEDURES
'reads an Ini string
Public Function ReadIni(Filename As String, Section As String, Key As String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileString(Section, Key, "", RetVal, 255, Filename)
ReadIni = Left(RetVal, v + 0)
End Function
'reads an Ini section
Public Function ReadIniSection(Filename As String, Section As String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileSection(Section, RetVal, 255, Filename)
ReadIniSection = Left(RetVal, v + 0)
End Function
How can I use this to create a function that basically allows me to specify only the section I want to look in, and then find each ini string within that section and put it into an array and return that Array so I can do a loop with it?
我如何使用它来创建一个函数,该函数基本上只允许我指定我想要查看的部分,然后在该部分中找到每个 ini 字符串并将其放入一个数组并返回该数组,以便我可以使用它?
Edit: I see that ReadIniSection returns all of the keys in a huge string. Meaning, I need to split it up.
编辑:我看到 ReadIniSection 返回一个巨大字符串中的所有键。意思是,我需要把它分开。
ReadIniSection returns something that looks like this: "Fornavn=FORNAVN[]Etternavn=ETTERNAVN" etc etc. The[] in the middle there isn't brackets, it's a square. Probably some character it doesn't recognize. So I guess I should run it through a split command that takes the value between a = and the square.
ReadIniSection 返回如下所示的内容:“Fornavn=FORNAVN[]Etternavn=ETTERNAVN”等等。中间的[] 没有括号,它是一个正方形。可能是它不认识的某些字符。所以我想我应该通过一个 split 命令来运行它,该命令取 a = 和平方之间的值。
回答by tpascale
See if this helps - splitting on nullchar \0:
看看这是否有帮助 - 在 nullchar \0 上拆分:
Private Sub ListIniSectionLines()
Dim S As String: S = ReadIniSection("c:\windows\win.ini", "MAIL")
Dim vLines As Variant: vLines = Split(S, Chr$(0))
Dim vLine As Variant
For Each vLine In vLines
Debug.Print vLine
Next vLine
End Sub