【问题标题】:Read entire ini section and put into array读取整个 ini 部分并放入数组
【发布时间】:2011-07-24 08:11:33
【问题描述】:

好的,我准备通过我的 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" 等等。中间的[]没有括号,它是一个正方形。可能是一些它不认识的字符。所以我想我应该通过一个拆分命令来运行它,该命令采用 a = 和正方形之间的值。

【问题讨论】:

    标签: arrays vba vbscript ini


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2011-05-30
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      相关资源
      最近更新 更多