我在使用ini文件的时候,用两个不同的可执行文件去读或写,有时候程序不能正常退出,总是在内存中,单步执行的时候,写Ini文件的那个API函数好像又不能写进去,换个关键字又可以写了,不只是什么原因,请各位指点。郁闷中。

解决方案 »

  1.   

    '读写INI的API函數
    Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As LongPublic 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函數
    Public Function WriteIni(ByVal section As String, ByVal key As String, ByVal value As String) As Boolean
        Dim X As Long, Buff As String * 128, I As Integer
        Buff = value + Chr(0)
        X = WritePrivateProfileString(section, key, Buff, App.Path + "\DTDB.ini")
        WriteIni = X
    End Function'自定义读取INI函數
    Public Function ReadIni(ByVal section As String, ByVal key As String) As String
        Dim X As Long, Buff As String * 128, I As Integer
        X = GetPrivateProfileString(section, key, "", Buff, 128, App.Path + "\DTDB.ini")
        I = InStr(Buff, Chr(0))
        ReadIni = Trim(Left(Buff, I - 1))
    End Function
      

  2.   

    1。在写Ini的时候,需要创建一个互斥进程吗?
    2。Ini文件支持同时写入吗?
    3。为什么有时候单步跟踪也是写不进去,是不是系统有什么要求阿。
      

  3.   

    如果ini文件很小得话完全没有必要用api,直接用数组读取就行了,要比api节省资源吧
      

  4.   

    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, 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 LongPrivate Sub Form_Load()
        Open "C:\aa.ini" For Output As #1
        Print #1, "文件内容"
        Close #1
    End SubPrivate Sub Label1_Click()
    On Error GoTo Errhandle
        ShellExecute Me.hwnd, "open", "www.sohu.com", vbNullString, vbNullString, 1
        Exit Sub
    Errhandle:
        MsgBox Err.Description & "!", vbInformation
    End Sub'写INI文件
    Private Sub Command1_Click()
        Dim Counter As Long    For Counter = 1 To 4
            Call WriteToIni(App.Path & "\Options.ini", "Test", "Name" & Counter, "Value" & Counter)
        Next Counter
    End Sub'读INI文件
    Private Sub Command2_Click()
        Dim Counter As Long
        Dim Value(3) As String    For Counter = 1 To 4
            Value(Counter - 1) = ReadFromIni(App.Path & "\Options.ini", "Test", "Name" & Counter)
        Next Counter
    End Sub