读取整个 ini 部分并放入数组中

发布于 2024-10-27 09:36:16 字数 1471 浏览 4 评论 0原文

好的,所以我尝试通过 vba 代码使用这些函数。 vbs 可能也是如此。

这是函数

'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

我如何使用它来创建一个函数,基本上允许我只指定我想要查看的部分,然后找到该部分中的每个 ini 字符串并将其放入一个数组中并返回该数组,这样我可以用它做一个循环吗?

编辑:我看到 ReadIniSection 返回一个巨大字符串中的所有键。 意思是,我需要把它分开。

ReadIniSection 返回如下所示的内容: “Fornavn=FORNAVN[]Etternavn=ETTERNAVN”等等。中间的[]没有括号,它是一个正方形。可能是一些它无法识别的字符。所以我想我应该通过 split 命令来运行它,该命令采用 a = 和平方之间的值。

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.

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?

Edit: I see that ReadIniSection returns all of the keys in a huge string.
Meaning, I need to split it up.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑丑阿 2024-11-03 09:36:16

看看这是否有帮助 - 在 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

See if this helps - splitting on 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文