我有一个 ini 文件,我想从中获取所有日期。目前我所拥有的只是第一次约会。我尝试过循环运行我的函数,但它仍然只给了我第一个日期。
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim passfail = GetIniValue("DATE", "self-test-test.ini", "FAIL")
RichTextBox3.Text = passfail
End Sub
Private Sub RichTextBox3_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox3.TextChanged
End Sub
Public Function GetIniValue(key As String, filename As String, Optional defaultValue As String = "") As String
Dim inifile As New StreamReader(filename)
Dim LineData As String
Dim textdata() As String
Dim keydata As String = defaultValue
Dim done As Boolean = False
If File.Exists(filename) Then
Do While Not done
If inifile.Peek() >= 0 Then
LineData = inifile.ReadLine()
textdata = Split(LineData, "=")
If Trim(textdata(0)) = key Then
If UBound(textdata) > 0 Then
keydata = Trim(textdata(1))
End If
done = True
End If
Else
done = True
End If
Loop
inifile.Close()
End If
GetIniValue = keydata
End Function
End Class
> ini file self-test-test.ini
[20E2SN5]
DATE=05/01/2019,15:12:12
STATUS=PASS
[20R5SN9]
DATE=06/21/2019,15:13:13
STATUS=PASS
这是我尝试过的循环之一,但仍然只给了我第一次约会。
``` Dim passfail = GetIniValue("DATE", "self-test-test.ini", "FAIL")
For Each n In passfail
RichTextBox3.Text += n.ToString
Next
currently I am getting a return of
05/01/2019,15:12:12
but would like to receive a return of
05/01/2019,15:12:12
06/21/2019,15:13:13
使用 Windows API
GetPrivateProfileSectionNames
获取所有部分名称,然后使用 GetPrivateProfileString
获取“DATE”键的值。
您可以使用下面的辅助函数(
GetIniSectionNames
和 GetIniValue
)。
Private Declare Function GetPrivateProfileSectionNames Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" (lpszReturnBuffer As IntPtr, nSize As UInteger, lpFileName As String) As UInteger
Private Sub GetIniSectionNames(iniFilename As String, ByRef sectionNames() As String)
Const MAX_BUFFER As UInteger = 32767
Dim pReturnedString = Marshal.AllocCoTaskMem(MAX_BUFFER)
Dim bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, iniFilename)
If bytesReturned = 0 Then sectionNames = Nothing
Dim local As String = Marshal.PtrToStringAnsi(pReturnedString, bytesReturned).ToString
Marshal.FreeCoTaskMem(pReturnedString)
sectionNames = local.Substring(0, local.Length - 1).Split(vbNullChar)
End Sub
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (lpSectionName As String, lpKeyName As String, lpdefaults As String, lpReturnedstring As String, nSize As Integer, lpFileName As String) As Integer
Private Sub GetIniValue(iniFilename As String, iniSection As String, iniKey As String, defaultValue As String, ByRef exists As Boolean, ByRef value As String)
Dim lngResult As Integer
Dim buffer As String = New String(" ", 255)
Dim bufferSize As Integer = buffer.Length
lngResult = GetPrivateProfileString(iniSection, iniKey, defaultValue, buffer, bufferSize, iniFilename)
If lngResult > 0 Then
exists = True
Else
exists = False
End If
value = buffer.Substring(0, lngResult)
End Sub