从INI文件读取 [英] Reading from an INI file

查看:141
本文介绍了从INI文件读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I find it very easy to write to a INI file, but am having some trouble in retrieving the data from an already created INI file.

我正在使用此功能:

    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文件,其中包含以下数据:

If I have an INI file called 'c:\temp\test.ini', with the following data:

[testApp]
KeyName=keyValue
KeyName2=keyValue2

如何检索KeyName和KeyName2的值?

How can I retrieve the values of KeyName and KeyName2?

我尝试过此代码,但没有成功:

I have tried this code, with no success:

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

推荐答案

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

Going to the Pinvoke.Net Web site and modifying their example worked, their Function declaration is different.

修改后的示例

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

这篇关于从INI文件读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆