繁体   English   中英

从INI文件读取

[英]Reading from an INI file

我发现写入INI文件非常容易,但是从已经创建的INI文件中检索数据时遇到了一些麻烦。

我正在使用此功能:

    Public Declare Unicode Function GetPrivateProfileString Lib "kernel32" _
    Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, _
    ByVal lpKeyName As String, ByVal lpDefault As String, _
    ByVal lpReturnedString As String, ByVal nSize As Int32, _
    ByVal lpFileName As String) As Int32

如果我有一个名为'c:\\ temp \\ test.ini'的INI文件,其中包含以下数据:

[testApp]
KeyName=keyValue
KeyName2=keyValue2

如何检索KeyName和KeyName2的值?

我尝试了这段代码,但没有成功:

    Dim strData As String
    GetPrivateProfileString("testApp", "KeyName", "Nothing", strData, Len(strData), "c:\temp\test.ini")
    MsgBox(strData)

转到Pinvoke.Net网站并修改其示例后,它们的Function声明就不同了。

修改示例

Imports System.Runtime.InteropServices
Imports System.Text
Module Module1
    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

    Sub Main()

        Dim res As Integer
        Dim sb As StringBuilder

        sb = New StringBuilder(500)
        res = GetPrivateProfileString("testApp", "KeyName", "", sb, sb.Capacity, "c:\temp\test.ini")
        Console.WriteLine("GetPrivateProfileStrng returned : " & res.ToString())
        Console.WriteLine("KeyName is : " & sb.ToString())
        Console.ReadLine();

    End Sub
End Module

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM